react-useref
React useRef
Overview
useRef is a "box" for a value that survives between renders but doesn't trigger re-renders when changed. Two distinct use cases: DOM access and mutable instance values.
useState → change = re-render
useRef → change = NO re-render, React doesn't know
The Rule
If changing the value should update UI → useState. If not → useRef.
Case 1: DOM Access
The most common and cleanest use case — imperative DOM methods:
function SearchModal() {
const inputRef = useRef(null);
useEffect(() => { inputRef.current?.focus(); }, []);
return <input ref={inputRef} placeholder="Search..." />;
}
Also: scrollIntoView, ResizeObserver, measuring element dimensions.
Case 2: Mutable Values Between Renders
Store values that shouldn't cause re-renders:
// Timer ID
const intervalRef = useRef(null);
const start = () => {
intervalRef.current = setInterval(() => setTime(t => t + 1), 1000);
};
const stop = () => clearInterval(intervalRef.current);
// Previous value
const prevPriceRef = useRef(price);
useEffect(() => { prevPriceRef.current = price; });
const diff = price - prevPriceRef.current;
// First render flag
const isFirstRender = useRef(true);
useEffect(() => {
if (isFirstRender.current) { isFirstRender.current = false; return; }
fetchData(filters); // skip on mount, run on filter change
}, [filters]);
Case 3: Stable Callback in useEffect (React 19.2+)
Old workaround with useRef is replaced by useEffectEvent:
// ❌ Old workaround (before React 19.2)
const onSuccessRef = useRef(onSuccess);
useEffect(() => { onSuccessRef.current = onSuccess; });
useEffect(() => {
fetchData().then(data => onSuccessRef.current(data));
}, []);
// ✅ Modern (React 19.2+)
import { useEffectEvent } from 'react';
const stableOnSuccess = useEffectEvent(onSuccess);
useEffect(() => {
fetchData().then(data => stableOnSuccess(data));
}, []);
Note: useEffectEvent can only be called inside effects, not in UI event handlers.
Case 4: External Library Integration
Libraries outside React (D3, Chart.js, Google Maps) need direct DOM access:
function D3Chart({ data }) {
const svgRef = useRef(null);
useEffect(() => {
if (!svgRef.current) return;
const svg = d3.select(svgRef.current);
svg.selectAll('*').remove();
// D3 manipulates DOM directly
}, [data]);
return <svg ref={svgRef} width={600} height={400} />;
}
Decision Table
| Question | Answer |
|---|---|
| Should change update UI? | useState |
| Need DOM node reference? | useRef |
| Storing timer/interval ID? | useRef |
| Tracking previous value? | useRef |
| First-render flag? | useRef |
| Integrating non-React library? | useRef |
| Value changes but doesn't affect render? | useRef |
Anti-pattern
// ❌ useRef as "hidden useState" — UI won't update
const filtersRef = useRef([]);
const applyFilter = (filter) => {
filtersRef.current.push(filter); // React doesn't know — UI stays stale
};
// ✅ If it affects UI, use useState
const [filters, setFilters] = useState([]);
const applyFilter = (filter) => {
setFilters(prev => [...prev, filter]);
};
References
- useRef — React docs — official API reference covering DOM access and mutable values
- useEffectEvent — React docs — official reference for the hook that replaces the useRef callback workaround
More from b4r7x/agent-skills
react-design-patterns
Use when choosing a React component pattern — custom hooks, control props, compound components, headless components, render props, container/presentational, or other architectural patterns. Includes 13 patterns with decision guide and 2025 popularity ranking.
26human-commit
Generates human-like git commit messages based on staged or unstaged changes. Reads git diff, analyzes what changed, and outputs 3 natural commit message options that sound like they were written by a developer — not AI. This skill should be used when the user wants a commit message, asks "what should I write for commit", "generate commit message", "human like commit", "wiadomość do commita", or just asks for help committing.
24humanize-readme
Rewrites a README.md to remove AI slop — buzzwords, generic openers, fake enthusiasm, and formulaic structure — replacing it with direct, honest, human-sounding writing. This skill should be used when the user wants to humanize a README, remove AI-generated writing patterns, make documentation sound less like ChatGPT wrote it, or asks to "fix the README", "humanize readme", "remove AI slop", "make it sound human".
24improve-prompt
Transforms a rough, unpolished prompt idea into a precise, structured AI coding prompt. Automatically researches the current project context (stack, file structure, conventions, git history) before generating. This skill should be used when the user provides a vague or "dirty" prompt idea and asks to refine, improve, or rewrite it — e.g. "improve this prompt", "refine my prompt", "ulepszony prompt", "dopracuj prompt", or simply describes what they want done in rough terms.
23react-anti-patterns
Use when reviewing React code — especially AI-generated code — to catch common anti-patterns. Covers 18 anti-patterns with detection difficulty, including stale closures, state mutation, useEffect abuse, and boolean explosion.
21deep-plan
Takes a rough, unpolished prompt idea and autonomously turns it into an implementation plan. Researches the project deeply, asks clarifying questions, generates a precise internal prompt, then executes it to produce a structured plan with todos. Designed for plan mode. Use when the user gives a vague feature request, rough idea, or "dirty" prompt and wants a ready-to-execute implementation plan — e.g. "plan this", "deep plan", "turn this into a plan", "zaplanuj to", "zrób plan".
19