Maaz Zindani

Loading the index…

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

setState runs while React is rendering a class component

ErrorWarning: 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.

  • Applies to React 19
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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]*=\{[^}(]*\(" src
  2. 2Match what you see, then jump to its fix

    1. 1The warning appears on page load, before any clickThe handler is called in JSX instead of passedFix
    2. 2The call sits in the class body of render() or constructor(), not in a methodsetState is called inside render or inside the constructorFix
    3. 3The class has componentWillUpdate or UNSAFE_componentWillUpdate, and a setState or a dispatch from itsetState is triggered from componentWillUpdateFix

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={.*()}" src

2Fix

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 dev

Confirmed by react.dev

Did this fix it?

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.jsx

2Fix

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 dev

Confirmed by react.dev

Did this fix it?

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" src

2Fix

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 dev

Confirmed by react.dev

Did this fix it?

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:

Related fixes

Next step

Want this handled for good?

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