This React error means a component sets state while rendering, which triggers another render forever. See the usual causes, like calling a handler instead of passing it, and how to fix them.
The short answer
#Something in your component is updating state during render. Each update causes another render, which updates state again, so React stops the loop and throws. Most often you are calling a function in JSX instead of passing it. Wrap it in an arrow function so it only runs on click.
_10- <button onClick={setCount(count + 1)}>Add</button>_10+ <button onClick={() => setCount(count + 1)}>Add</button>
Why it happens
#A React component is a function that runs every time it renders. Anything you write in the function body, or inside the JSX it returns, runs on every render. A state setter such as setCount asks React to render again with the new value.
If a setter runs unconditionally during render, you get a cycle: render, set state, render, set state. React allows a small number of these updates during render, then throws this error rather than freezing the page.
The common causes are:
- Calling a handler in a prop:
onClick={handleClick()}runshandleClickwhile rendering and passes its return value as the prop. - Setting state directly in the component body, for example to copy a prop into state.
- Setting state inside a render-time calculation, such as inside a
.mapover items.
A similar error, "Maximum update depth exceeded", appears when the loop goes through an effect instead, for example a useEffect that sets state on every run without a dependency array.
How to fix it
#Pass functions, do not call them
If the handler needs an argument, wrap it in an arrow function.
_13function TodoList({ todos }: { todos: Todo[] }) {_13 const [selected, setSelected] = useState<string | null>(null);_13_13 return (_13 <ul>_13 {todos.map((todo) => (_13 <li key={todo.id}>_13 <button onClick={() => setSelected(todo.id)}>{todo.title}</button>_13 </li>_13 ))}_13 </ul>_13 );_13}
Derive values instead of storing them
If you are setting state from props or other state during render, you usually do not need state at all. Calculate the value as a normal variable.
_12// Before: loops forever_12function Basket({ items }: { items: Item[] }) {_12 const [total, setTotal] = useState(0);_12 setTotal(items.reduce((sum, item) => sum + item.price, 0));_12 return <p>Total: {total}</p>;_12}_12_12// After: no state needed_12function Basket({ items }: { items: Item[] }) {_12 const total = items.reduce((sum, item) => sum + item.price, 0);_12 return <p>Total: {total}</p>;_12}
If the calculation is expensive, wrap it in useMemo. If you use the React Compiler, it can memoise this for you.
Guard updates that must happen during render
Occasionally you do need to adjust state when a prop changes, such as resetting a selection when the list changes. React supports this if the update is behind a condition that stops it repeating.
_10const [prevItems, setPrevItems] = useState(items);_10if (items !== prevItems) {_10 setPrevItems(items);_10 setSelection(null);_10}
Often a cleaner option is to give the component a key so React resets all its state when the key changes.
Give effects the right dependencies
For the effect version of the loop, add a dependency array and make sure the effect does not set a value that is also one of its dependencies, unless it checks first.
Common pitfalls
#- Writing
onClick={handleClick(id)}because it reads naturally. It runs immediately. - Using an object or array literal as an effect dependency. It is a new value on every render, so the effect runs every time.
- Copying props into state with
useEffectand then deriving more state from it. Each step adds another render. - Fixing the symptom with a ref flag. Find the update that runs during render and move it to an event handler or derive the value instead.
Related reading
#Was this resource helpful?