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 setState unconditionally in the component body.

How to fix it

  1. Pass a function to event handlers:

                          // Wrong
    <button onClick={setOpen(true)}>Open</button>
    // Right
    <button onClick={() => setOpen(true)}>Open</button>
                        
  2. Move state that depends on other values into an event handler, a useEffect with correct dependencies, or compute it during render without storing it in state.

Let’s talk about the next step

Have an idea, a stuck app, or a workflow toimprove?

Tell me what you are building, what is blocking you, and what a successful outcome looks like.