javascript - Is React's setState asynchronous or something? -


hmm. i'm using setstate , reason code following doesn't have access new state!

what gives?!

yeap. it's asynchronous. i'm posting because isn't obvious new react users.

react "queues" updates component's state.

if need execute code block that's dependent on new state change, pass callback so:

getinitialstate: function () {   return {     isfinalcountdown: false,   } }  //blablabla  //then somewhere got... this.setstate(   {isfinalcountdown: true},   function () {//<--- whoa. solves your synchrosity woes!     console.log(this.state.isfinalcountdown); //true!   } );  console.log(this.state.isfinalcountdown); //false! 

all of in docs, it's needs reiterated avoid common bugs new react users come across.

check out: https://facebook.github.io/react/docs/component-api.html#setstate


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -