Fix guides · Lovable, Bolt, v0, Cursor, Claude Code, Replit ·
Fix "Maximum update depth exceeded" in React
Error: Maximum update depth exceeded. This can happen when a component repeatedly calls setState inside componentWillUpdate or componentDidUpdate. React limits the number of nested updates to prevent infinite loops.
"Maximum update depth exceeded" means a component keeps updating its own state in a loop, so React stops it. In function components this is almost always a useEffect that sets state on every run, because one of its dependencies (an object, array or function created during render) is new every time. Make the dependency stable or remove the effect, and the loop stops.
Common causes – and how to tell which one you have
1. useEffect sets state and depends on that same state
How to tell: An effect like useEffect(() => setItems([...items, x]), [items]) — it changes a value that is in its own dependency array, so every run schedules the next one.
Fix: Use the functional form setItems(prev => ...) and remove the state from the dependency array, or move the logic into the event handler that actually causes the change.
2. An object, array or function in the dependency array is recreated on every render
How to tell: The dependency is something like const options = { page: 1 }, const filters = [], or a function defined inside the component. The console warning or the React DevTools Profiler shows the component rendering nonstop.
Fix: Move constants outside the component, depend on primitive values (options.page instead of options), or wrap the value in useMemo and the function in useCallback with correct dependencies.
3. Copying props into state with an effect
How to tell: An effect like useEffect(() => setValue(props.value), [props]) or one that derives state from props — the parent re-renders, passes a new object, the child sets state, and so on.
Fix: Compute the value directly during render (const total = items.reduce(...)) instead of storing it in state. Only keep real user input in state.
4. A callback prop that updates the parent on every render
How to tell: The child calls onChange(...) or onLoad(...) inside a useEffect, and the parent passes an inline arrow function that sets parent state.
Fix: Call the callback only when the value really changed (compare with the previous value), wrap the parent's handler in useCallback, or call it from the user event instead of an effect.
5. setState called directly in a ref callback or a library's onChange
How to tell: The stack trace points into a form, table, chart or map library, or into a ref={el => setNode(el)} callback.
Fix: Only set state when the value differs from the current one (if (el !== node) setNode(el)), or use useRef instead of state for values that don't need to re-render the UI.
How to fix it
- Open the browser console and expand the error or the warning above it. Find the first component name from your own code in the stack.
- In that component, look at every useEffect that calls a setter (setX). Note what is in its dependency array.
- For each dependency, ask: is it an object, array or function created during render? If yes, that's the loop.
- Make it stable: move constants outside the component, depend on primitive fields, or use useMemo/useCallback.
- If the effect only computes a value from props or state, delete the effect and the state and compute the value during render.
- Reload the page and check that the error is gone and the component renders once or twice, not continuously (React DevTools Profiler helps).
Using Lovable, Bolt or v0?
In Lovable, Bolt or v0, paste the full error with the component name and say: "This is an infinite render loop. Find the useEffect in this component that sets state, show me its dependency array, and fix the unstable dependency instead of adding a guard." These builders often create filter objects or fetch functions inside the component and put them in the dependency array, which is exactly what causes this.
Using Cursor, Claude Code or Windsurf?
Ask Cursor or Claude Code to list every useEffect in the failing component with its setters and dependencies, and to mark which dependencies get a new identity on every render. Have it fix the cause, then confirm with React DevTools (Profiler or 'Highlight updates') that the component stops re-rendering.
The “fix” to avoid
Agents often add a boolean like hasRun or a render counter to stop the loop, or delete dependencies and add // eslint-disable-next-line react-hooks/exhaustive-deps. That hides the loop but leaves stale data and new bugs. Make the dependency stable or remove the effect instead.
Copy-paste fix prompt for your AI coding tool
Paste this into Cursor, Claude Code, Lovable or Bolt:
My React app throws "Maximum update depth exceeded". Find the component in the stack trace and check every useEffect that calls a state setter. For each one, list its dependencies and tell me which is recreated on every render (object, array or inline function) or is the state the effect itself sets. Fix the loop properly: use functional updates (setX(prev => ...)), depend on primitive values, move constants outside the component, wrap values in useMemo/useCallback, or remove effects that only derive state from props. Do not disable the exhaustive-deps lint rule and do not add a counter or flag to stop the loop. Tell me which effect caused it.
Questions people ask
Is this the same as "Too many re-renders"?
They're related. "Too many re-renders" usually means setState is called directly during render (for example onClick={setOpen(true)}). "Maximum update depth exceeded" usually comes from an effect or a lifecycle method that keeps setting state after each render.
Why does it say componentDidUpdate when I only use hooks?
The message text is older than hooks. In function components the same thing happens through useEffect or useLayoutEffect setting state on every run.
It only shows as a warning and the app still works. Can I ignore it?
No. The component is still rendering in a loop, which burns CPU, can spam API calls and makes the page slow. Fix the dependency.