In React, useRef is a special Hook that helps you "reference" or keep track of a DOM element or a value in your functional components without causing re-renders.
But what does that mean?
Imagine you have a magic notebook where you can write down a note and always refer back to it, no matter how many times you change the pages. In this analogy, useRef is like that magic notebook. It lets you keep a persistent note (or reference) without changing how your component behaves.
Before Hooks, if you wanted to access a DOM element or maintain some mutable value without causing re-renders, you had to use class components and deal with more complex code. With useRef, you can do this in functional components easily.
The useRef Hook is useful for:
In summary, useRef provides a way to interact with elements and values directly, without affecting the component's rendering.
Let’s walk through how the useRef Hook works, step by step. Here’s a basic example:
import React, { useRef } from 'react';
function ExampleComponent() {
// 1. Create a ref object
const inputRef = useRef(null);
const handleClick = () => {
// 2. Access the DOM element
inputRef.current.focus();
};
return (
<div>
<input ref={inputRef} type="text" />
<button onClick={handleClick}>Focus the input</button>
</div>
);
}
export default ExampleComponent;
Let’s break this code into smaller pieces to understand it fully:
At the top of the file, we import the useRef Hook from React. You always need to import Hooks like useRef before using them in your component.
import React, { useRef } from 'react';
Inside your functional component (ExampleComponent), you create something called a ref object. The useRef Hook helps you do this.
const inputRef = useRef(null);
This line does a few important things:
inputRef: This is the ref object. It will be used to store a reference to the input element. The null is the initial value of the ref object.useRef(null): This tells React to create a ref object that starts as null. When the component renders, React will assign the ref to the DOM element you reference.Inside the handleClick function, we access the input element using inputRef.current. This is like using your magic notebook to get the note you wrote earlier.
const handleClick = () => {
inputRef.current.focus();
};
Calling inputRef.current.focus() focuses the input field when the button is clicked.
We attach the ref to the input element using the ref attribute.
<input ref={inputRef} type="text" />
By setting ref={inputRef}, React links the ref object to this input element, allowing us to access it through inputRef.current.
Let’s examine what’s happening:
useRef(null) creates a ref object with an initial value of null.inputRef.current to interact with the element.This is how useRef helps you keep track of elements or values without causing unnecessary re-renders.
useState.Here are some examples of how you might use useRef in real-life scenarios:
const inputRef = useRef(null);
<input ref={inputRef} type="text" />
<button onClick={() => inputRef.current.focus()}>Focus Input</button>
inputRef holds a reference to the input element, allowing you to focus it programmatically.
const countRef = useRef(0);
const increment = () => {
countRef.current += 1;
console.log(countRef.current);
};
countRef keeps track of a count value that can be updated without causing re-renders.