I am aware setState can be synchronous or asynchronous:
setState()does not immediately mutate this.state but creates a pending state transition. Accessingthis.stateafter calling this method can potentially return the existing value.There is no guarantee of synchronous operation of calls to
setStateand calls may be batched for performance gains.
Right, and I understand the function executes a few things:
- Update the actual value in the state
- Update the virtual and real DOM as needed with the new state values
But what I'm trying to figure out is why this is sometimes synchronous and sometimes asynchronous? What happens behind the scene? Why can't it be always synchronous?
Thanks.