React & Next.js errors · Error 01 of 7
“Too many re-renders” in React
By Muhammad Abdullah, full-stack, mobile and ai automation developer · Last reviewed
Too many re-renders. React limits the number of renders to prevent an infinite loop.Short answer
A state update runs during render, which triggers another render, forever. The usual cause is calling a setter directly in JSX, like onClick={setOpen(true)}, instead of passing a function.
Common causes
- Calling a state setter in an event prop instead of passing a callback.
- Calling
setStateunconditionally in the component body.
How to fix it
Pass a function to event handlers:
// Wrong <button onClick={setOpen(true)}>Open</button> // Right <button onClick={() => setOpen(true)}>Open</button>Move state that depends on other values into an event handler, a
useEffectwith correct dependencies, or compute it during render without storing it in state.