Sub topic 9
Common pitfalls
This chapter assumes you know useState, basic useEffect, and how useRef attaches to a DOM element. It covers the mistakes that cause most ref bugs and how to debug them.
Reading a ref before it is set
A ref created with useRef(null) is null until React mounts the element. During the first render, ref.current is still null. The same is true for elements that are not rendered yet.
import { useRef } from 'react';
export function Broken() {
const inputRef = useRef(null);
inputRef.current.focus(); // TypeError: inputRef.current is null
return <input ref={inputRef} />;
}Move the access into an event handler or an effect. Both run after React has attached the node.
import { useEffect, useRef } from 'react';
export function Fixed() {
const inputRef = useRef(null);
useEffect(() => {
inputRef.current?.focus();
}, []);
return <input ref={inputRef} />;
}The ?. guards against elements that render conditionally. If the element may not exist, check for null before you use it.
Using a ref for data that appears on screen
Changing ref.current does not trigger a render. If the value is shown in the JSX, the screen goes stale.
import { useRef } from 'react';
export function BrokenCounter() {
const count = useRef(0);
return (
<button onClick={() => { count.current += 1; }}>
Clicked {count.current} times
</button>
);
}The value increases, but the text stays at 0 until something else causes a re-render. Use useState for anything the user sees. Keep refs for values that only your code reads, such as timer IDs.
Touching ref.current during render
Reading or writing ref.current in the body of a component makes the output depend on something React cannot track. Rendering stops being predictable, and behavior can change under Strict Mode or concurrent rendering. Do this work in event handlers and effects. The one exception is lazy initialization, where you set the value only if it is still null.
Other mistakes to watch for
| Symptom | Likely cause | Fix |
|---|---|---|
| `ref.current` is `null` in a handler | The element is conditionally rendered and not mounted | Check for `null`, or render the element |
| Ref on a custom component is `null` | The child does not pass `ref` to a DOM node | Forward the ref (or accept `ref` as a prop in React 19) |
| Timer keeps running after unmount | No cleanup | Clear it in the effect's cleanup function |
| Callback ref runs on every render | Inline function is a new function each time | Define the callback once, or accept the extra calls |
| Stale value inside an interval | Callback closed over an old render | Store the latest callback in a ref |
An inline callback ref is called with null and then with the node on every render, because React sees a new function each time. This is harmless for simple bookkeeping, but it matters if the callback does expensive work.
Debugging a null ref
Exercise
A component shows {renderCount.current} where renderCount = useRef(0) and increments it in a click handler. The number on screen never changes. Why, and what is the fix?
Show answer
Changing ref.current does not trigger a re-render, so React never redraws the text. Because the value is displayed, store it in state with useState and update it with the setter.