useRef Hook
Learn how to use the useRef Hook to store values between renders and access DOM elements without causing a re-render when the reference changes.
1. What Is the useRef Hook?
The useRef Hook is a built-in React Hook that returns
a ref object containing a property called
current. The value of this property can be
changed without causing the component to re-render.
It is commonly used to access DOM elements, focus input fields, store timer IDs, and keep mutable values between renders.
ref.current does not automatically
update the UI. Use useState when a value
needs to trigger a re-render.
2. Syntax of useRef
The basic syntax is:
import { useRef } from "react";
const myRef = useRef(initialValue);
- useRef: The React Hook used to create a ref object.
- initialValue: The initial value stored in the ref.
- myRef.current: The property used to read or update the stored value.
3. How useRef Works
React creates a ref object that persists between renders. You can read or update its current value without asking React to render the component again.
4. Example: Accessing an Input Element
In this example, useRef stores a reference to an input element. When the button is clicked, the input field receives focus.
Example Code
import { useRef } from "react";
function App() {
const inputRef = useRef(null);
function focusInput() {
inputRef.current.focus();
}
return (
<div>
<h2>Focus Input Example</h2>
<input
ref={inputRef}
type="text"
placeholder="Enter your name"
/>
<button onClick={focusInput}>
Focus Input
</button>
</div>
);
}
export default App;
Expected Output
Focus Input Example
[ Enter your name ]
[ Focus Input ]
After clicking the button, the cursor moves to the input field.
inputRef object is attached to the input
through the ref attribute. After React
attaches the DOM element, inputRef.current
refers to that element, allowing the code to call
focus().
5. Example: Storing a Value with useRef
A ref can store a mutable value between renders. Updating that value does not trigger a re-render.
Example Code
import { useRef } from "react";
function App() {
const clickCount = useRef(0);
function handleClick() {
clickCount.current += 1;
console.log(
"Button clicked:",
clickCount.current,
"times"
);
}
return (
<div>
<h2>useRef Counter</h2>
<button onClick={handleClick}>
Click Me
</button>
</div>
);
}
export default App;
Expected Output
useRef Counter
[ Click Me ]
After three clicks, the console shows:
Button clicked: 3 timesThe count is stored in the ref and is not displayed automatically in the component.
clickCount.current value increases with
each click. Because changing a ref does not trigger a
re-render, the count is logged to the console instead of
being automatically displayed in the UI.
6. Difference Between useRef and useState
- useState: Stores state values and schedules a re-render when its setter updates the state.
- useRef: Stores a mutable value that persists between renders without triggering a re-render when updated.
- useState example: Displaying a counter value that changes on screen.
- useRef example: Focusing an input field or storing a timer ID.
7. Important Points About useRef
-
Import
useReffrom React. - Call Hooks at the top level of a component or custom Hook.
- The ref object persists between renders.
-
Read or update the value through
ref.current. - Updating a ref does not automatically re-render the component.
- DOM refs are useful for focus, scrolling, and other imperative browser operations.
8. Practice Tasks
Complete the following exercises:
- Create an input field and focus it using useRef.
- Create a button that focuses an email input field.
- Store the number of button clicks in a ref and log the value to the console.
- Store a timer ID in a ref and clear the timer when required.
- Explain the difference between useRef and useState.