Fix guides · Lovable, Bolt, v0, Cursor, Claude Code ·
Fix React "Too many re-renders" infinite loop
Too many re-renders. React limits the number of renders to prevent an infinite loop.
This React error means a component sets state during rendering, which triggers another render, which sets state again, forever. React stops it after a limit. It's almost always an event handler written as onClick={setOpen(true)} instead of onClick={() => setOpen(true)}, or a setState call placed directly in the component body instead of in an event handler or useEffect.
Common causes – and how to tell which one you have
1. Handler called instead of passed
How to tell: You have something like onClick={setCount(count + 1)} or onClick={handleClick()} with parentheses. The function runs during render instead of on click.
Fix: Pass a function: onClick={() => setCount(count + 1)} or onClick={handleClick} without parentheses.
2. setState directly in the component body
How to tell: A line like setItems(data) or setLoading(false) sits at the top level of the component, not inside a handler, useEffect or callback.
Fix: If the value can be computed from props or other state, compute it as a normal variable during render instead of storing it in state. If it really reacts to a change, move it into a handler or a useEffect with the right dependencies.
3. Copying props into state on every render
How to tell: The component does if (props.value !== value) setValue(props.value) without a guard that eventually stops it, or with an object or array that's new each render.
Fix: Use the prop directly instead of copying it into state. If you need to reset state when a prop changes, give the component a key based on that prop.
4. Parent and child update each other
How to tell: The error points at a child that calls a parent's setter during render, for example onChange(value) in the child's body.
Fix: Call the parent's setter only in an event handler in the child, or lift the state so only one component owns it.
How to fix it
- Open the browser console and look at the component name in the stack trace under the error.
- In that component, search for onClick=, onChange= and onSubmit= that contain a call with parentheses, such as onClick={setOpen(true)} or onClick={handleSave()}.
- Change each one to pass a function: onClick={() => setOpen(true)} or onClick={handleSave}.
- Look for setState calls that sit directly in the component body. Replace them with a computed variable (for example const total = items.length) or move them into an event handler.
- If a prop is copied into state, use the prop directly or reset the component with a key prop instead.
- Reload the page and confirm the error is gone and the buttons still do what they should.
Using Lovable, Bolt or v0?
In Lovable, Bolt or v0, tell the AI: "I get 'Too many re-renders'. Find any onClick or onChange that calls a function directly (like onClick={setOpen(true)}) and any setState in the component body, and fix them without adding a useEffect workaround." Copy the component name from the error stack into the chat so it looks at the right file.
Using Cursor, Claude Code or Windsurf?
Ask Cursor or Claude Code to open the component named in the stack trace and list every setState call and where it runs (render, handler, effect). Have it fix the ones that run during render, then run the app and confirm the page loads without the error and that clicking the affected buttons still works.
The “fix” to avoid
Agents often move the setState into a useEffect without a dependency array, or add a boolean flag to stop it after the first time. That swaps this error for 'Maximum update depth exceeded' or for a hidden loop that wastes renders and causes flicker. Remove the state update from rendering instead, or derive the value without state.
Copy-paste fix prompt for your AI coding tool
Paste this into Cursor, Claude Code, Lovable or Bolt:
My React app crashes with "Too many re-renders. React limits the number of renders to prevent an infinite loop." Open the component from the stack trace and check, in this order: 1) event props that call a function instead of passing one (onClick={setX(...)} or onClick={fn()}), 2) setState calls directly in the component body, 3) props copied into state on every render, 4) a child calling a parent's setter during render. Fix them by passing functions (onClick={() => setX(...)}), deriving values with plain variables or useMemo instead of state, or moving updates into event handlers. Do not just move the setState into a useEffect without dependencies or add a 'has run' flag. Explain which line caused the loop and confirm the page loads without the error and the interactions still work.
Questions people ask
What's the difference between 'Too many re-renders' and 'Maximum update depth exceeded'?
'Too many re-renders' means state is set during rendering itself. 'Maximum update depth exceeded' usually means a useEffect sets state and re-runs every time, often because it has no dependency array or a dependency that changes on every render.
Why does onClick={setOpen(true)} cause an infinite loop?
The parentheses call setOpen immediately while rendering. That causes a new render, which calls it again. Writing onClick={() => setOpen(true)} only runs it when clicked.
Should I put the update in useEffect instead?
Only if it truly reacts to something changing, and then with a correct dependency array. Most of the time the value can be computed during render and doesn't need state at all.