Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

React: Maximum update depth exceeded or Too many re-renders

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

Short answer

React stops a component that sets state again and again without end. The usual causes are an event handler called during render, an effect that sets state it also depends on, and an object or function recreated every render and used as an effect dependency.

  • Applies to React 19, React Native 0.7x, Expo SDK 5x
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Open the red screen's stack trace and find the set function (setX) that appears in it. That line is the one that keeps running.

    grep -rn "setState\|set[A-Z][A-Za-z]*(" src app --include=*.tsx --include=*.js | head -n 40
  2. 2Match what you see, then jump to its fix

    1. 1Too many re-renders, and a prop like onPress={handlePress()} with bracketsA handler is called during render instead of passedFix
    2. 2The set call sits inside useEffect and the state is in the [] arrayAn effect sets state that is also in its dependency listFix
    3. 3The effect sets state and lists an object, array or function declared in the component bodyAn object or function created in render is an effect dependencyFix

What does the error look like?

  • Too many re-renders. React limits the number of renders to prevent an infinite loop.
  • Maximum update depth exceeded

Cause 1 of 3

A handler is called during render instead of passed

1Check

A call with brackets runs while rendering. If the handler sets state, every render triggers another render.

grep -rn "on[A-Z][A-Za-z]*={[A-Za-z]*()}" src app --include=*.tsx --include=*.js

2Fix

Pass the function itself, or an inline arrow function.

// wrong
<Pressable onPress={handlePress()} />
// right
<Pressable onPress={handlePress} />
<Pressable onPress={() => handlePress(id)} />

3Verify

Reload. The error stops and the handler runs only on press.

npx expo start --clear

Confirmed by react.dev

Did this fix it?

Cause 2 of 3

An effect sets state that is also in its dependency list

1Check

Each run changes the state, the render changes the dependency, and the effect runs again.

grep -rn -A6 "useEffect(" src app --include=*.tsx --include=*.js | head -n 60

2Fix

Ask whether the effect is needed. If it is, update from the previous value or remove the self-referencing dependency.

useEffect(() => {
  setCount(c => c + 1); // no [count] dependency needed
}, []);

3Verify

The component renders a fixed number of times.

console.log('render'); // temporary, in the component body

Confirmed by react.dev

Did this fix it?

Cause 3 of 3

An object or function created in render is an effect dependency

1Check

These are new on every render, so React treats them as changed and runs the effect after every commit.

grep -rn -B10 "^ *}, \[" src app --include=*.tsx --include=*.js | head -n 60

2Fix

Create the object or function inside the effect. As a last resort wrap it in useMemo or useCallback.

useEffect(() => {
  const options = { roomId };   // created inside the effect
  connect(options);
}, [roomId]);

3Verify

The effect now runs only when roomId changes.

console.log('effect ran'); // temporary, inside the effect

Confirmed by react.dev

Did this fix it?

How do I stop it coming back?

  • Never call a set function in the body of a component unless it is guarded by a condition.
  • Pass handlers as references, not as calls.
  • Keep effect dependencies to primitive values where you can.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Web apps

Expo app never leaves the splash screen

App shows the splash screen forever and no error appears

After SplashScreen.preventAutoHideAsync() the splash stays until you hide it yourself.

3 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.