javascript - Calling setState in a loop only updates state 1 time -
is there reason calling setsate() in loop prevent updating state multiple times?
i have a basic jsbin highlights problem seeing. there 2 buttons. 1 updates state's counter 1. other calls underlying function of 1 in loop -- seemingly update state multiple times.
i know of several solutions problem want make sure understanding underlying mechanism here first. why can't setstate called in loop? have coded awkwardly preventing desired effect?
from react docs:
setstate() not mutate this.state creates pending state transition. accessing this.state after calling method can potentially return existing value. there no guarantee of synchronous operation of calls setstate , calls may batched performance gains.
basically, don't call setstate in loop. what's happening here docs referring to: this.state returning previous value, pending state update has not been applied yet.
Comments
Post a Comment