Skip to main content

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*

16
  • 10
    Can you provide an example of when a race condition would occur? Commented Feb 11, 2015 at 2:12
  • 3
    @Qiming push returns the new array length so that won’t work. Also, setState is async and React can queue several state changes into a single render pass. Commented Jul 1, 2015 at 9:34
  • 2
    @mindeavor say you have an animationFrame that looks for parameters in this.state, and another method that changes some other parameters on state change. There could be some frames where the state has changed but not reflected in the method that listens for the change, because setState is async. Commented Dec 21, 2016 at 13:06
  • 1
    @ChristopherCamps This answer does not encourage calling setState twice, it shows two similar examples of setting state array without mutating it directly. Commented Feb 8, 2017 at 12:34
  • 3
    An easy way to treat a state array as immutable these days is: let list = Array.from(this.state.list); list.push('woo'); this.setState({list}); Modify to your style preferences of course. Commented Mar 3, 2017 at 16:57