Skip to main content
  1. About
  2. For Teams

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

Required fields*

Does React keep the order for state updates?

I know that React may perform state updates asynchronously and in batch for performance optimization. Therefore you can never trust the state to be updated after having called setState. But can you trust React to update the state in the same order as setState is called for

  1. the same component?
  2. different components?

Consider clicking the button in the following examples:

1. Is there ever a possibility that a is false and b is true for:

class Container extends React.Component {
  constructor(props) {
    super(props);
    this.state = { a: false, b: false };
  }

  render() {
    return <Button onClick={this.handleClick}/>
  }

  handleClick = () => {
    this.setState({ a: true });
    this.setState({ b: true });
  }
}

2. Is there ever a possibility that a is false and b is true for:

class SuperContainer extends React.Component {
  constructor(props) {
    super(props);
    this.state = { a: false };
  }

  render() {
    return <Container setParentState={this.setState.bind(this)}/>
  }
}

class Container extends React.Component {
  constructor(props) {
    super(props);
    this.state = { b: false };
  }

  render() {
    return <Button onClick={this.handleClick}/>
  }

  handleClick = () => {
    this.props.setParentState({ a: true });
    this.setState({ b: true });
  }
}

Keep in mind that these are extreme simplifications of my use case. I realize that I can do this differently, e.g. updating both state params at the same time in example 1, as well as performing the second state update in a callback to the first state update in example 2. However, this is not my question, and I am only interested in if there is a well defined way that React performs these state updates, nothing else.

Any answer backed up by documentation is greatly appreciated.

Answer*

Cancel
19
  • 3
    One way to "always get the order right" is to create a temporary object, assign the different values (e.g obj.a = true; obj.b = true) and then at the end just do this.setState(obj). This is safe regardless if you are inside an event handler or not. Might be a neat trick if you often find yourself making the mistake of setting the state several times outside of event handlers.
    Chris
    –  Chris
    2018-02-12 13:43:32 +00:00
    Commented Feb 12, 2018 at 13:43
  • 1
    And by the way that also means we should not read from this.state at all; the only reasonable way to read some state.X is to read it in updater function, from its argument. And, writing to this.state is also unsafe. Then why allow access to this.state at all? Those should maybe made standalone questions but mostly I am just trying to understand if I got the explanation right.
    Maksim Gumerov
    –  Maksim Gumerov
    2018-02-22 11:39:38 +00:00
    Commented Feb 22, 2018 at 11:39
  • 26
    this answer should be added to reactjs.org documentation
    Deen John
    –  Deen John
    2018-03-10 02:53:54 +00:00
    Commented Mar 10, 2018 at 2:53
  • 4
    Could you please clarify in this post if "React event handler" includes componentDidUpdate and other lifecycle callbacks as of React 16? Thanks in advance!
    Ivan
    –  Ivan
    2020-02-14 11:48:46 +00:00
    Commented Feb 14, 2020 at 11:48
  • 1
    Updated the answer for React 18: localhost:8000/blog/2022/03/29/…
    Dan Abramov
    –  Dan Abramov
    2022-04-06 23:57:42 +00:00
    Commented Apr 6, 2022 at 23:57

Morty Proxy This is a proxified and sanitized view of the page, visit original site.