setState runs while React is rendering a class component
Warning: setState(...): Cannot update during an existing state transition (such as within `render`).Short answer
React warns when setState runs during render or in the constructor, because render must be a pure function of props and state. The usual cause is a handler called in JSX, onClick={this.save()}, instead of passed, onClick={this.save}. Pass the function, or wrap it in an arrow.
Fix it fast
1Find out which cause you have
Search the file for an event prop that ends in a call, like onClick={this.something()} or onChange={fn(value)}. Every such call runs on each render.
grep -rn -E "on[A-Z][A-Za-z]*=\{[^}(]*\(" src2Match what you see, then jump to its fix
- 1
The warning appears on page load, before any clickThe handler is called in JSX instead of passedFix - 2
The call sits in the class body of render() or constructor(), not in a methodsetState is called inside render or inside the constructorFix - 3
The class has componentWillUpdate or UNSAFE_componentWillUpdate, and a setState or a dispatch from itsetState is triggered from componentWillUpdateFix
- 1
What does the error look like?
Warning: setState(...): Cannot update during an existing state transition (such as within `render`).Too many re-renders. React limits the number of renders to prevent an infinite loop.
Cause 1 of 3
The handler is called in JSX instead of passed
1Check
Writing onClick={handleClick()} fires the function immediately while rendering. The React docs say what React needs is a function to call later: onClick={handleClick} or an inline arrow.
grep -rn "onClick={.*()}" src2Fix
Remove the call parentheses, or wrap the call in an arrow function when you must pass an argument.
// wrong: runs on every render
<button onClick={this.setOpen(true)}>Open</button>
// right
<button onClick={() => this.setOpen(true)}>Open</button>3Verify
Reload. The warning is gone and the state changes on click.
npm run devConfirmed by react.dev
Cause 2 of 3
setState is called inside render or inside the constructor
1Check
The React docs say render should be a pure function of props, state and context with no side effects, and that you must not call setState in the constructor.
grep -n -A12 "render()\|constructor(" src/components/Panel.jsx2Fix
Set the initial value by assigning this.state in the constructor, and move other state changes into an event handler or componentDidMount.
constructor(props) {
super(props);
this.state = { open: false };
}
componentDidMount() {
this.setState({ ready: true });
}3Verify
Reload the page and check the console.
npm run devConfirmed by react.dev
Cause 3 of 3
setState is triggered from componentWillUpdate
1Check
The React docs say it is not supported to call setState, or anything that leads to it such as dispatching a Redux action, during componentWillUpdate.
grep -rn "componentWillUpdate" src2Fix
Move the update to componentDidUpdate and guard it with a comparison of previous and current props.
componentDidUpdate(prevProps) {
if (prevProps.userId !== this.props.userId) {
this.setState({ loaded: false });
}
}3Verify
Change the prop that was involved and check the console.
npm run devConfirmed by react.dev
How do I stop it coming back?
- Read every event prop as passing a function. If it ends in (), it is a call.
- Keep render free of side effects, as the React docs require.
- Use componentDidUpdate with a props comparison for state that follows a prop.
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
React warns about updating one component while another renders
To locate the bad setState() call inside `Child`, follow the stack trace
React renders must be pure.
4 causes, each with a fixWeb apps
Module not found for antd/dist/antd.less after upgrading to Ant Design 5
Module not found: Error: Can't resolve 'antd/dist/antd.less'
Ant Design 5 dropped Less and the bundled CSS file for CSS-in-JS, so antd/dist/antd.less no longer exists.
3 causes, each with a fixWeb apps
Astro Code component fails inside a React component
Astro components cannot be rendered directly via function call
Astro components such as Code only exist on the server and render static HTML.
2 causes, each with a fixWeb apps
React shows Attempted import error when importing a component or function
Attempted import error:
The build found the file but not the name you asked for.
2 causes, each with a fixWeb apps
How to catch and handle a 422 from Axios in a Redux thunk
action.error.message is 'Rejected' and action.payload is undefined
A 422 means the server read the request but cannot process its content, so repeating it unchanged fails again.
4 causes, each with a fixWeb apps
axios gets 429 Too Many Requests from a React app
429 (Too Many Requests)
A 429 comes from the server or an API gateway, not from axios: your client sent too many requests in a window.
4 causes, each with a fix