Every React codebase has one mysterious bug where the screen shows old data, nothing errors, and the fix turns out to be a ref holding something the UI was supposed to display:
const count = useRef(0);
<button onClick={() => { count.current++; }}>
Clicked {count.current} times
</button>
// click forever: the badge never updates. No error. No warning.
// the mutation never told React to look.The developer reached for useRef because "it avoids re-renders", which is the ref's most quoted feature and its most common misuse. The line between ref and state is one question.
Step 1: Ask who needs to see the change
If the UI should update when the value changes, it is state. If nothing on screen depends on the value, it is a ref:
// UI depends on it -> state
const [count, setCount] = useState(0);
// nobody draws it -> ref
const timerId = useRef<ReturnType<typeof setInterval>>();That is the entire decision. State changes schedule renders. Ref changes mutate a box. When the UI depends on the box, it renders the box's value from last render, forever, with no error to debug.
Step 2: Default to state when unsure
State in the wrong place costs a re-render. A ref in the wrong place costs a stale screen and an afternoon:
// ref holding a visible UI value: the bug
const nameRef = useRef(form.values.name);
return <p>Welcome, {nameRef.current}</p>;
// form updates, screen never does, no error anywhereThe asymmetry makes the default obvious: reach for state, and downgrade to a ref only when you can name the re-render you are preventing. Performance is the wrong reason anyway. If renders are actually slow, memoization, smaller components, or moving state closer to where it is used beat hiding data from React every time.
Step 3: Reach for refs for the DOM first
Focus, scroll position, measuring, media playback. This is the ref's home turf:
function SearchBox() {
const inputRef = useRef<HTMLInputElement>(null);
return (
<>
<input ref={inputRef} placeholder="Search…" />
<button onClick={() => inputRef.current?.focus()}>
Focus search
</button>
</>
);
}Most legitimate refs in a real codebase hold DOM nodes. If your ref holds a value instead of a node, be suspicious of it.
Step 4: Store what survives renders, not what drives them
Interval ids, previous values, mounted flags. The ref remembers between renders without making React re-render:
function useTicker() {
const intervalId = useRef<ReturnType<typeof setInterval>>();
useEffect(() => {
intervalId.current = setInterval(tick, 1000);
return () => clearInterval(intervalId.current);
// state here would re-render on every id assignment
// and the id would be unreachable in cleanup anyway
}, []);
}function usePrevious<T>(value: T) {
const prev = useRef<T>();
useEffect(() => {
prev.current = value;
}, [value]);
return prev.current;
}Notice both examples write the ref inside useEffect, never in the render body. That is not a style choice; it is the next rule.
Step 5: Never read or write refs during render
Refs belong in event handlers and effects:
// wrong: write during render, breaks Strict Mode double-invocation
const prev = useRef<string>();
prev.current = name; // render body = forbidden zone
// right: write in an effect
const prev = useRef<string>();
useEffect(() => {
prev.current = name;
}, [name]);Reading during render makes the output depend on mutable data React cannot see, and writing during render breaks the moment Strict Mode double-invokes. Render stays pure, refs change outside it, and the whole system stays predictable.
The decision fits in one sentence: if React needs to react to the value, state. If it is bookkeeping nobody draws, a ref. If your bug of the month is a screen showing stale data, audit the refs first. Somewhere in there is a value that should have been state.
React app with mystery stale-UI bugs or a render loop nobody can explain? This is usually a one-day audit. Tell me about the symptoms and I will tell you where I would look first.