The useRef Hook
Mutable references that persist across renders, accessing DOM elements, and storing values.
What you'll learn
- What useRef is and how it differs from useState
- Accessing DOM elements directly with refs
- Storing mutable values that do not trigger re-renders
- When to use a ref vs state vs a variable
Concept
What Is useRef?
useRef returns a mutable ref object whose .current property persists across re-renders. Unlike state, changing a ref does not trigger a re-render. This makes refs useful for storing values that React does not need to track for rendering purposes. The ref object stays the same (same identity) across every render, so you can safely mutate .current without worrying about stale closures.
Accessing DOM Elements
The most common use of refs is to access a DOM element directly. You pass the ref to a DOM element's ref prop, and React sets ref.current to the actual DOM node after mount. This lets you call imperative methods like focus(), scrollIntoView(), getBoundingClientRect(), or measure an element. This is the escape hatch for when you need to interact with the DOM in ways that declarative JSX cannot express.
Storing Mutable Values
Refs are also useful for storing any mutable value that should not trigger a re-render when it changes. Common examples include timer IDs (setInterval returns), previous values for comparison, and caches. Because mutating .current does not cause a re-render, the UI will not update to reflect the change — this is intentional. If the value should affect the UI, use useState instead.
Refs vs State vs Variables
A regular variable declared in the component body is re-created on every render, so it cannot persist values. State persists and triggers re-renders. Refs persist but do not trigger re-renders. Use a variable for values used only within a single render. Use state for values that should update the UI when they change. Use a ref for values that need to persist but should not update the UI directly.
forwardRef and useImperativeHandle
Sometimes a parent needs a ref to a child component's internal DOM element. Since React 19, you can pass refs as regular props, but in earlier versions you need forwardRef. The useImperativeHandle hook lets you customize what the parent sees when it accesses the ref — instead of the DOM node, you can expose specific methods. This is useful for creating reusable input components that expose focus() or clear() methods to parents.
Example
import { useRef, useState, useEffect } from "react";
function Stopwatch() {
const [seconds, setSeconds] = useState(0);
const intervalRef = useRef(null); // Store timer ID.
const inputRef = useRef(null); // Access DOM input.
const start = () => {
if (intervalRef.current) return; // Prevent duplicate intervals.
intervalRef.current = setInterval(() => {
setSeconds(s => s + 1);
}, 1000);
};
const stop = () => {
clearInterval(intervalRef.current);
intervalRef.current = null;
};
// Focus the input on mount using the DOM ref.
useEffect(() => {
inputRef.current?.focus();
}, []);
return (
<div>
<input ref={inputRef} placeholder="Label for your timer" />
<p>Elapsed: {seconds}s</p>
<button onClick={start}>Start</button>
<button onClick={stop}>Stop</button>
</div>
);
}
intervalRef stores the timer ID without triggering re-renders. inputRef accesses the DOM input to call focus() imperatively on mount.
Try it
- Code Diff
Compare ref usage patterns when refactoring component logic.
- JSON Formatter
Inspect data structures you might store in refs.
Common mistakes
The mistake
Using a ref for state that should trigger a re-render
The fix
Changing ref.current does not cause a re-render. If the UI should update when the value changes, use useState instead. Refs are for values that persist but do not affect rendering.
The mistake
Reading or writing ref.current during render
The fix
React forbids reading or writing refs during render. Read and write refs in event handlers, effects, or callbacks — not in the component body during render.
Practice The useRef Hook
6 exercises
Look up unfamiliar terms
A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.
Build real projects
Apply what you learned by building guided projects with starter code and solutions.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.