vendredi 17 février 2017

How does react update state?

I'm getting a very strange issue that I don't know how to address.

I've got a fairly straightforward method: When a button is pressed it called toggleInverted()

toggleInverted() {
  if(this.state.inverted){
    this.setState({inverted: false});
  } else{
    console.log("got to else...");
    this.setState({inverted: true});
    console.log(this.state.inverted);
  }
}

The inverted field is initialized in the constructor to false. However the first time I click the button when I load the page, it doesn't correctly reset the state. The output is: got to else... false.

So somehow it is getting into this else statement, executing the setState, and yet not setting inverted to be true...

Is there something about setState that I am missing?

Aucun commentaire:

Enregistrer un commentaire