Skip to content

Error: Too many re-renders. React limits the number of renders to prevent an infinite loop

Error message

                Too many re-renders. React limits the number of renders to prevent an infinite loop.
              

Quick summary

You called a state setter function directly in the component body (not in an event handler or effect), causing an infinite render loop.

Why this happens

  • Called setState directly in the component body

  • Called setState in a function that runs on every render

  • Used a setter as a default value for another useState

Minimal example

✗ Broken code
function Component() {
  const [count, setCount] = useState(0);
  setCount(count + 1); // Called every render!
  return <div>{count}</div>;
}
✓ Fixed code
function Component() {
  const [count, setCount] = useState(0);
  return <button onClick={() => setCount(count + 1)}>{count}</button>;
}

Calling setCount during render triggers a re-render, which calls setCount again — infinite loop. Moving it to onClick breaks the cycle.

How to diagnose

  • Look for setState calls outside event handlers and useEffect

  • Check if a setter is called during every render

How to fix

  • Move the setState call into an event handler

  • Move the setState call into a useEffect with proper dependencies

  • Use a lazy initializer if computing initial state: useState(() => compute())

How to prevent

  • Never call setState in the render body

  • Use lazy initializers for expensive initial state computations

Related Resources

Related Glossary

Related Lessons

  • State

    Learn to avoid infinite render loops.

← Back to language