Accepted answer

from react's documentation:

setstate() does not immediately mutate this.state but creates a pending state transition. accessing this.state after calling this method can potentially return the existing value. there is no guarantee of synchronous operation of calls to setstate and calls may be batched for performance gains.

if you want a function to be executed after the state change occurs, pass it in as a callback.

this.setstate({value:}, function () {


simply putting - this.setstate({data: value}) is asynchronous in nature that means it moves out of the call stack and only comes back to the call stack unless it is resolved.

please read about event loop to have a clear picture about asynchronous nature in js and why it takes time to update -

hence -

    console.log(; // will give undefined or unupdated value

as it takes time to update. to achieve the above process -

    this.setstate({data:value},function () {


react bathces different set state calls so that it can determine what is the most optimal strategy for rerendering the website is going to be.

imagine you have an application where you have a lot of different components. perhaps, with one button click you are updating the state in multiple components, not just on the current one. in this case, react does not want to just completely isolate and do all those different updates independently.

react wants to figure out if it can stack all these updates together, maybe there is a more optimal way of updating these components so that it is more performant. this is what react is doing behind the scenes. as a result, set state call is asynchronous call.


accessing this.state after calling the setstate method is not guaranteed to return the updated status due to the asynchronous nature of setstate.

to guarantee an update after calling setstate, there are two solutions you may pursue.

solution 1: as mentioned in one of the above answers, put your code in the componentdidupdate method

solution 2: as mentioned in another of the above answers, pass your stuff as a callback

 this.setstate({value: myvalue}, function () {

it's important to note that these two solutions are not clearly interchangeable. the one cannot easily solve all the use-cases of the other. as a general rule, if you can, best practice says that solution 1 is preferred. but, there are use-cases where only solution 2 "more effectively" works such as the "update-my-view-and-post-my-data" use case. this use case goes like this:

after adding an item, say, "add schedule", i want to both add that item to a front-end list and immediately post the just-updated-list to the backend, as demonstrated in the concept below: enter image description here

if you dont do either solution, i.e. if you only say this in your code:

addtoitemarray = () => { 
     this.setstate{{ scheduleditemsarray: newobjectlistwithmax}}   

<button classname="btn btn-secondary btn-block" onclick={this.addtoitemarray}>add shedule</button>

... you will post the list excluding the "delivery to max" item, because the state wont be updated when you this.postdata() (again, because its asynchronous).

if you utilise solution 1, you would make a post after typing in every character in the schedule name textbox!

there are other ways aswell to cater for this use-case but solution 2 best conveys the intent when reading the code.

given the ubiquitous nature of this use case in virtually every web app, the callback technique explained by michael's answer is an indispensable piece of code in every developers toolkit.


async-await syntax works perfectly for something like the following...

changestatefunction = () => {
  // some worker..

  this.setstate((prevstate) => ({
  year: funchandleyear(),
  month: funchandlemonth()

gonextmonth = async () => {
  await this.changestatefunction();
  const history = createbrowserhistory();

goprevmonth = async () => {
  await this.changestatefunction();
  const history = createbrowserhistory();


watch out the react lifecycle methods!

i worked for several hours to find out that getderivedstatefromprops will be called after every setstate().



you could try using es7 async/await. for instance using your example:

handlechange: async function(event) {
    await this.setstate({value:});


as mentioned in the react documentation, there is no guarantee of setstate being fired synchronously, so your console.log may return the state prior to it updating.

michael parker mentions passing a callback within the setstate. another way to handle the logic after state change is via the componentdidupdate lifecycle method, which is the method recommended in react docs.

generally we recommend using componentdidupdate() for such logic instead.

this is particularly useful when there may be successive setstates fired, and you would like to fire the same function after every state change. rather than adding a callback to each setstate, you could place the function inside of the componentdidupdate, with specific logic inside if necessary.

// example
componentdidupdate(prevprops, prevstate) {
  if (this.state.value > prevstate.value) {;  

Related Query

More Query from same tag