Skip to content

Error: Maximum update depth exceeded

Error message

                Maximum update depth exceeded. This can happen when a component repeatedly calls setState inside componentWillUpdate or componentDidUpdate.
              

Quick summary

A component is stuck in an infinite loop of state updates and re-renders, usually because setState is called during render or in a useEffect with wrong dependencies.

Why this happens

  • Called setState directly during render (not in an event handler or effect)

  • useEffect with a dependency that changes on every render

  • useEffect that always calls setState without a condition

  • A parent and child updating each other's state in a loop

Minimal example

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

Calling setState during render triggers a re-render, which calls setState again — infinite loop. Moving it to an event handler breaks the cycle.

How to diagnose

  • Look for setState calls in the render body (not inside event handlers)

  • Check useEffect dependency arrays for values that change every render

  • Add console.log in render to see how many times it fires

How to fix

  • Move setState into an event handler or useEffect

  • Fix the dependency array to avoid including values that change every render

  • Add a condition before setState in useEffect to avoid unnecessary updates

How to prevent

  • Never call setState during render

  • Use the exhaustive-deps ESLint rule to catch dependency issues

  • Be careful with object/array dependencies that create new references

Related Resources

Related Glossary

Related Lessons

  • State

    Learn to avoid infinite render loops.

← Back to language