React
frontendreactGuideBeginner
The useRef Hook in React
Mastering useRef: persisting values without triggering re-renders, DOM element referencing, focus management, and storing timer IDs.
Verified: 2026-09-30
Tested on:react 19.x
Platforms:
web
#react#hooks#use-ref#dom#focus
The useRef Hook in React
useRef is a React hook that lets you reference a value that persists across renders without triggering a new re-render when mutated.
1. Referencing DOM Elements (e.g. Focus / Scroll)
import { useRef } from "react";
export function SearchInput() {
// 1. Initialize ref with null
const inputRef = useRef<HTMLInputElement>(null);
function handleFocus() {
// 2. Access underlying DOM node via .current
inputRef.current?.focus();
}
return (
<div>
<input ref={inputRef} type="text" placeholder="Type here..." />
<button type="button" onClick={handleFocus}>
Focus Input
</button>
</div>
);
}2. Storing Mutable Data Without Re-Rendering
Use useRef when you need to track information (like timer IDs or previous state snapshots) that should not affect the visual UI:
import { useState, useRef, useEffect } from "react";
export function Stopwatch() {
const [seconds, setSeconds] = useState(0);
const timerRef = useRef<NodeJS.Timeout | null>(null);
function start() {
if (timerRef.current !== null) return;
timerRef.current = setInterval(() => {
setSeconds(s => s + 1);
}, 1000);
}
function stop() {
if (timerRef.current) {
clearInterval(timerRef.current);
timerRef.current = null;
}
}
useEffect(() => {
return () => stop(); // Cleanup timer on unmount
}, []);
return (
<div>
<p>Time: {seconds}s</p>
<button type="button" onClick={start}>Start</button>
<button type="button" onClick={stop}>Stop</button>
</div>
);
}Related Topics
The useEffect Hook in React
Mastering useEffect: synchronizing with external systems, cleanup functions, dependency array rules, and avoiding unnecessary effects.
The useMemo Hook in React
Optimizing expensive calculations with useMemo: caching computed values, referential equality, and when not to over-optimize.