React: Maximum update depth exceeded or Too many re-renders
Too 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.
Fix it fast
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 402Match what you see, then jump to its fix
- 1
Too many re-renders, and a prop like onPress={handlePress()} with bracketsA handler is called during render instead of passedFix - 2
The set call sits inside useEffect and the state is in the [] arrayAn effect sets state that is also in its dependency listFix - 3
The 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
- 1
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=*.js2Fix
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 --clearConfirmed by react.dev
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 602Fix
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 bodyConfirmed by react.dev
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 602Fix
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 effectConfirmed by react.dev
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:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- TaskvareA task and project-management suite: React on the web, Flutter on mobile, TypeScript underneath.
Related fixes
Web apps
Axios get works with a URL string but ignores the object passed as the second argument
The server log shows a GET with no query string
axios.get takes a URL and an optional config object.
3 causes, each with a fixWeb 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 fixWeb apps
NativeViewGestureHandler must be used as a descendant of GestureHandlerRootView
Error: NativeViewGestureHandler must be used as a descendant of GestureHandlerRootView. Otherwise the gestures will not be recognized.
react-native-gesture-handler only recognises gestures inside GestureHandlerRootView.
3 causes, each with a fixWeb apps
React Hook "useState" cannot be called at the top level, or in a function that is not a component
The lint points at a useState line inside a helper function, an onClick handler or the top of a module
The lint rule found a hook outside a component or custom hook.
3 causes, each with a fixWeb apps
React Native says the application has not been registered
Application has not been registered
The native side asks JavaScript to run an app name that JavaScript never registered.
3 causes, each with a fixWeb apps
TypeScript marks className as an error on React Native components
Property 'className' does not exist on type
React Native components have no className prop in their own types.
3 causes, each with a fix