In React, we use Hooks like useState to store and update data.
But sometimes, we need to store a value without causing the component to re-render.
We may also need to directly access an HTML element, such as an input field or button.
This is where useRef comes in.
1. What is useRef?
useRef is a React Hook that allows us to store a value that stays between renders.
It is commonly used for two things:
- Accessing HTML elements directly.
- Storing values without causing a re-render.
In simple words, useRef helps us remember values or access elements without triggering a re-render when the ref changes.
2. How to use useRef?
First, import useRef from React.
import { useRef } from "react";
Then create a ref inside your component.
const myRef = useRef(null);
A ref contains a property called current.
We can access or update its value using:
myRef.current
3. Accessing an HTML Element Using useRef
Let's understand with a simple example.
Imagine we have an input field and want to automatically focus on it when we click a button.
import { useRef } from "react";
function App() {
// Creating a ref
const inputRef = useRef(null);
function handleClick() {
// Focusing the input field
inputRef.current.focus();
}
return (
<div>
<input ref={inputRef} placeholder="Enter your name" />
<button onClick={handleClick}>
Focus Input
</button>
</div>
);
}
export default App;
How does it work?
-
useRef(null)creates a ref. - We connect the ref to the input using
ref={inputRef}. - When we click the button,
inputRef.currentgives us access to the input element. -
.focus()automatically places the cursor inside the input field.
This is one of the most common uses of useRef.
4. Storing Values Without Re-rendering
Another use of useRef is storing values without causing a component to re-render.
Let's see an example of a simple counter.
import { useRef } from "react";
function App() {
const countRef = useRef(0);
function handleClick() {
countRef.current++;
console.log(countRef.current);
}
return (
<div>
<h2>Click the button</h2>
<button onClick={handleClick}>
Increase
</button>
</div>
);
}
export default App;
Every time we click the button, the value inside countRef.current increases.
But notice something important:
The value is updated, but the component does not re-render.
We can see the updated value in the console, but the UI will not automatically display the new value.
If we need the UI to update whenever a value changes, we should use useState instead.
5. useRef vs useState
Both Hooks can store values, but they behave differently.
| useRef | useState |
|---|---|
| Stores a value between renders | Stores component state |
Updating .current does not cause a re-render |
Updating state causes a re-render |
| Commonly used to access HTML elements | Commonly used to update the UI |
Updated value is available through .current
|
Updated value is available through the state variable |
Example:
- Use
useStatewhen displaying a counter that updates on the screen. - Use
useRefwhen storing a counter value that doesn't need to update the screen.
6. Where Do We Use useRef?
Some common examples are:
- Input Fields: Automatically focus on an input field.
- DOM Elements: Access an HTML element directly.
-
Timers: Store a timer ID returned by
setInterval. - Previous Values: Store a previous value between renders.
- Tracking Values: Keep a value without triggering re-renders.
7. Important Points to Remember
-
useRefis a React Hook. - It stores values between renders.
- We access its value using
.current. - Updating
.currentdoes not trigger a re-render. - It can be used to access HTML elements directly.
- Use
useStatewhen a change needs to update the UI.
Top comments (0)