React Reference ยท Concepts and Pitfalls
Infinite render loops
Why React components loop forever or throw Too many re-renders: setState during render, effects that set state, unstable dependencies and how to fix each one.
An infinite loop in React means that a render causes a state update, which causes another render, which causes the same update again. React protects the browser from freezing. For updates made directly while rendering it throws "Too many re-renders. React limits the number of renders to prevent an infinite loop." For effects it eventually logs a "Maximum update depth exceeded" error in development, or the tab becomes unresponsive when the loop includes network requests.
There are only a few causes, and each is easy to recognize once you know it: calling a setter while rendering, an effect that sets state without limiting when it runs, and a dependency that is a new object or function on every render.
function Bad() { const [n, setN] = useState(0); setN(n + 1); // set during render: renders again, forever return <p>{n}</p>; }
Try it
The most common mistake is calling the setter instead of passing a function: onClick={setN(n + 1)} runs setN during render, because the parentheses call it immediately. The error boundary below catches React's error and prints the message.
The second cause is quieter. An effect whose dependency is an object created in the component body sees a "new" value every render, so it runs after every render. If it also sets state, the loop never ends. This demo caps the loop at five runs so the page stays usable, and then shows the fix.
Syntax
Typical causes and their fixes:
| Cause | Symptom | Fix |
|---|---|---|
onClick={doSomething()} or onClick={setX(1)} |
"Too many re-renders" at startup | Pass a function: onClick={() => setX(1)} or onClick={doSomething} |
| Setting state unconditionally in the component body | "Too many re-renders" | Move it into a handler or an effect, or compute the value instead |
| Effect with no dependency array that sets state | Renders keep repeating | Add a dependency array and set state only when needed |
| Effect depends on an object, array or function created during render | Effect runs after every render | Depend on primitive values, move the object outside the component, or wrap it in useMemo or useCallback |
Effect updates a value that is also its dependency (count in [count] while it calls setCount(count + 1)) |
Endless counting | Use an updater and remove the dependency, or add a condition that stops it |
| Parent and child effects that set each other's state | Ping-pong | Lift the state to one owner |
| Setting state in a ref callback with an inline function | Loop on every commit | Use a stable callback with useCallback |
useEffect(() => { if (data === null) { // a stopping condition setData(compute()); } }, [data]);
Usage notes
- Read the error's component stack. React names the component that keeps rendering; start there.
- Look at the last state update in the loop. Whatever is setting state unconditionally is the culprit.
- Setting state to the same value is cheap: React bails out when the new value is identical (
Object.is) and does not loop. Setting a fresh object or array every time defeats that. - Conditional state updates during render are allowed in one case: adjusting state from a previous prop, as long as the condition becomes false right after. Prefer deriving the value instead.
- Heavy loops hide behind fetches. An effect that fetches and sets state with an unstable dependency produces a request flood. Check the network panel when the page feels busy.
- The lint rule finds most of these.
react-hooks/exhaustive-depsreports dependencies that change on every render; see eslint-plugin-react-hooks. - StrictMode doubles effects on mount, which is not a loop. Two runs on first load is expected in development.