useRef
DOM access and mutable values that don't trigger re-renders
Skills taught: react.useRef, react.dom-refs, react.mutable-refs, react.forwardRef
useRef for DOM access
// Use case 1: Focus management
function SearchBox() {
const inputRef = useRef(null);
useEffect(() => {
inputRef.current?.focus(); // focus the input on mount
}, []);
const handleClear = () => {
inputRef.current.value = '';
inputRef.current.focus();
};
return (
<div style={{ display: 'flex', gap: '8px' }}>
<input
ref={inputRef} // React sets inputRef.current = this DOM node
type="text"
placeholder="Search..."
style={{ padding: '8px', border: '1px solid #d1d5db', borderRadius: '6px', flex: 1 }}
/>
<button onClick={handleClear}>Clear & Focus</button>
</div>
);
}
// Use case 2: Scroll into view
function ScrollDemo() {
const bottomRef = useRef(null);
const scrollDown = () => {
bottomRef.current?.scrollIntoView({ behavior: 'smooth' });
};
return (
<div style={{ height: '120px', overflow: 'auto', border: '1px solid #e2e8f0', borderRadius: '6px', padding: '8px' }}>
<button onClick={scrollDown}>Scroll to bottom</button>
{Array.from({ length: 10 }, (_, i) => <p key={i}>Line {i + 1}</p>)}
<div ref={bottomRef}>↑ Bottom reached</div>
</div>
);
}
function App() {
return (
<div style={{ fontFamily: 'sans-serif', padding: '8px', display: 'flex', flexDirection: 'column', gap: '12px' }}>
<SearchBox />
<ScrollDemo />
</div>
);
}
// Use case 3: Store interval ID without causing re-renders
function Interval() {
const [count, setCount] = useState(0);
const [running, setRunning] = useState(false);
const intervalRef = useRef(null); // store interval ID — NOT in state
const start = () => {
if (running) return;
setRunning(true);
intervalRef.current = setInterval(() => {
setCount(c => c + 1);
}, 500);
};
const stop = () => {
clearInterval(intervalRef.current);
setRunning(false);
};
return (
<div style={{ fontFamily: 'sans-serif', padding: '8px' }}>
<h2>Count: {count}</h2>
<button onClick={start} disabled={running}>Start</button>
<button onClick={stop} disabled={!running}>Stop</button>
</div>
);
}
// Use case 4: Track previous value
function usePrevious(value) {
const ref = useRef(undefined);
useEffect(() => { ref.current = value; }); // update AFTER render
return ref.current; // returns value from PREVIOUS render
}
function PreviousDemo() {
const [count, setCount] = useState(0);
const prev = usePrevious(count);
return (
<div style={{ fontFamily: 'sans-serif', padding: '8px' }}>
<p>Current: {count} | Previous: {prev ?? 'none'}</p>
<button onClick={() => setCount(c => c + 1)}>+1</button>
</div>
);
}
function App() {
return (
<div style={{ display: 'flex', flexDirection: 'column', gap: '12px' }}>
<Interval />
<PreviousDemo />
</div>
);
}
Frequently Asked Questions
When should I use useRef vs useState?
Use useState when the value should cause the UI to update when it changes. Use useRef when the value should persist across renders but NOT cause a re-render: timer IDs, DOM nodes, previous values, flags. Rule of thumb: if you'd ever display the value in JSX, it's state. If it's purely for internal bookkeeping or DOM access, it's a ref.