How setState works?

Christine Mathews

Christine Mathews / November 26, 2022

1 min read

To understand this, lets think about two things:

  1. Does setState function returns a promise ?The ans is No.
  2. Does the callback inside setState function is kind of an asynchronous callback? again no

So we can say that setState is definitely synchronous. but its behaviour seems like asynchronous due to the fact that it remembers the previous value from its closure.

for example if onClick you are setting a state using setState and performing some operations with it You might notice that the state has not updated at the same time because setState sets the value to that component's instance but due to closures it takes the previous value and the ops seems like asynchronous.