Maximum update depth exceeded in React: causes and how to fix it

IntermediateReact interview questionBy , updated 3 December 2026

Short answer

Maximum update depth exceeded means React saw too many state updates in a row, each one caused by the commit before it, so it stopped what looks like an infinite loop. It usually comes from a useEffect, useLayoutEffect or componentDidUpdate that sets state, where that state change makes the same code run again. Fix it with a correct dependency array, stable dependencies, a check before setting state, or by removing the effect.

What the error means

The message comes in two forms, and both describe the same loop: update, commit, run code that updates again.

  • A thrown error, React error #185: "This can happen when a component repeatedly calls setState inside componentWillUpdate or componentDidUpdate. React limits the number of nested updates to prevent infinite loops." In function components the same thing happens with useLayoutEffect, because it runs synchronously after the commit, just like componentDidUpdate.
  • A development warning for useEffect: "This can happen when a component calls setState inside useEffect, but useEffect either doesn't have a dependency array, or one of the dependencies changes on every render."

In React's current source both limits are 50 nested updates. The useEffect variant is only a warning in development. In production the loop is not stopped. The page just keeps re-rendering, burning CPU and often firing a network request on every pass.

The React docs put the cause in two conditions that must both be true: the effect updates some state, and that update leads to a re-render that changes the effect's dependencies.

Cause 1: an effect with no dependency array

useEffect(() => {
  setWidth(ref.current.offsetWidth);
}); // no array: runs after every render

With no array, the effect runs after every commit. If it sets state to a new value each time, it never settles. Add the dependencies the effect actually reads, or make the update conditional.

Cause 2: a dependency that is new on every render

function Results({ query }) {
  const [data, setData] = useState(null);
  const options = { query, limit: 20 }; // new object every render

  useEffect(() => {
    fetchResults(options).then(setData);
  }, [options]); // always "changed"
}

Objects, arrays and functions created during render are new each time, so React sees a changed dependency on every render. setData causes a render, the render makes a new options, and the effect runs again. Depend on the primitive values instead:

useEffect(() => {
  fetchResults({ query, limit: 20 }).then(setData);
}, [query]);

When you cannot avoid an object or function dependency, create it inside the effect or memoize it with useMemo or useCallback. Predict how many times effects run when dependencies are objects, arrays and functions drills exactly this.

Cause 3: the effect sets the state it depends on

useEffect(() => {
  setItems([...items, newItem]);
}, [items, newItem]); // changing items re-runs the effect

Use an updater function so the effect no longer reads items, and remove it from the array:

useEffect(() => {
  setItems((prev) => [...prev, newItem]);
}, [newItem]);

Often the better fix is to drop the effect. If the new item comes from a user action, add it in the event handler. If a value can be calculated from other state, calculate it during render.

Cause 4: a store selector that returns a new object

// A new object on every call
const { user, cart } = useStore((s) => ({ user: s.user, cart: s.cart }));

Hooks that read from an external store compare the result of your selector or getSnapshot with the last one. The React docs for useSyncExternalStore explain that returning a different object every time makes React re-render forever. Select each value separately, use your library's shallow comparison helper, or memoize the selector. Fix useSelector re-renders on every dispatch covers the Redux version.

Cause 5: componentDidUpdate without a comparison

componentDidUpdate(prevProps) {
  if (prevProps.userId !== this.props.userId) {
    this.setState({ profile: null });
  }
}

In a class component, every setState inside componentDidUpdate must be behind a condition that becomes false after the update. Without it, the update triggers another componentDidUpdate, and React throws error #185.

How to debug it

  1. Read the component stack in the console to find which component loops.
  2. Log each dependency of its effects and check which one changes on every run. A helper like useWhyDidYouUpdate prints the changed values for you.
  3. Ask, as the React docs suggest, whether the effect syncs with an external system at all. If not, it can probably be removed.

The useEffect dependencies and cleanup answer explains how React compares dependencies with Object.is.

Common follow-up questions

How is this different from "Too many re-renders"?

Too many re-renders is a loop inside one render, from calling a setter while rendering. Maximum update depth exceeded is a loop across commits, from effects and lifecycle methods.

Is an empty dependency array the fix?

Only if the effect really should run once. Using [] to stop the loop while the effect reads changing values gives you stale data instead. Fix the dependency, not the array.

Why does it only warn in development?

React only counts useEffect loops in development builds. In production nothing stops them, which is why a loop that "only warns" locally can hammer your API after release.

Can a ref stop the loop?

Yes, when the value is not needed for rendering. The React docs suggest a ref for data the UI does not show, because changing a ref does not trigger a render.

Mistakes to avoid

  • Leaving out the dependency array on an effect that sets state

  • Passing inline objects or functions as dependencies

  • Disabling the exhaustive-deps lint rule instead of fixing what it reports

  • Returning a new object from a store selector or getSnapshot

  • Calling setState in componentDidUpdate without comparing to the previous props or state

Practise it in code

Interviewers rarely stop at the definition. These questions open in an editor with tests.

  1. Debug an infinite useEffect() loop and prevent stale asynchronous responsesMedium35 min
  2. Predict how many times effects run when dependencies are objects, arrays and functionsMedium20 min
  3. Build linked inputs without effect loops: a Celsius/Fahrenheit converter and a recipe scalerMedium35 min
  4. Write a useWhyDidYouUpdate hook that logs which props changedMedium30 min
  5. Fix useSelector re-renders on every dispatch: createSelector, shallowEqual and selector factoriesMedium40 min
  6. Build a Zustand-style create() with a bound hook, useShallow and subscribeWithSelector middlewareMedium45 min
  7. Implement a fine-grained external state store using useSyncExternalStore without tearingHard60 minFree