Say you have two functions in your React app that call setState(), and are dependant one on the other. you might stumble upon a case where the second function excepts to have the new state – but doesn’t.
That happens because setState() doesn’t immediately mutate the state (this.state) but creates a pending state transition. therefore, accessing this.state might return the existing value.
to overcome this, this.setState supports a callback function to be triggered once it’s been updated.
this.setState(newStateObj, () => {});