A useEffect infinite loop happens when an effect changes state or a value that is also in its dependency array, so every run triggers a re-render that triggers the effect again. To fix it, find which dependency changes on every render, then stabilise it, remove it from the effect, or stop setting state unconditionally.
The three classic causes are a missing dependency array, state that is both updated by and listed in the effect, and objects, arrays or functions created during render that are used as dependencies. This guide shows each one with a fix.
React re-runs an effect after a render whenever any value in its dependency array differs from the previous render, compared with Object.is. If the effect itself causes another render and the dependencies change again, the cycle never ends:
In the browser this shows up as a frozen tab, a flood of network requests, or the console warning "Maximum update depth exceeded".
Without a second argument, the effect runs after every render:
Add a dependency array. If the effect should only run on mount, use an empty array:
If you only wanted to update the count in response to something, do it in an event handler instead of an effect.
Two fixes work, depending on intent:
If a value can be calculated from props or other state, do not store it in state with an effect at all. Compute it during render.
Objects, arrays and functions are new references on every render, even when their contents are the same:
Fixes, in order of preference:
[role].useMemo or the function in useCallback when it must be passed down.React 19 with the React Compiler can memoise many of these values automatically, but you should still not rely on it to hide a bad dependency list.
Listing items, the state you are setting, makes it a loop. Remove it from the dependencies; the effect reads only props.items. Better still, derive it:
react-hooks/exhaustive-deps lint rule on. Suppressing it with a comment is how many loops are introduced.It stops the loop, but it can read stale values. If the effect uses a prop or state, include it, or use a functional state update so you do not need it.
Usually it sets state that changes a dependency, or a dependency is an object or function re-created every render. Depend on primitives and keep the fetch inside the effect.
Yes, if it triggers a change in a parent through a callback prop and that parent passes a new object or function back down each time.
No. The warning usually points at the real cause. Restructure the code so the dependencies are honest and stable.
An infinite loop in useEffect means the effect keeps causing the render that re-runs it. Add a dependency array, depend on primitives instead of objects created during render, use functional state updates, and compute derived values during render instead of in effects. Keep the lint rule enabled to catch the next one early.