-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Open
Labels
Description
在React中,如果是由React引发的事件处理(比如通过onClick引发的事件处理),调用setState不会同步更新this.state,除此之外的setState调用会同步执行this.state 。所谓“除此之外”,指的是绕过React通过addEventListener直接添加的事件处理函数,还有通过setTimeout/setInterval产生的异步调用。
原因: 在React的setState函数实现中,会根据一个变量isBatchingUpdates判断是直接更新this.state还是放到队列中回头再说,而isBatchingUpdates默认是false,也就表示setState会同步更新this.state,但是,有一个函数batchedUpdates,这个函数会把isBatchingUpdates修改为true,而当React在调用事件处理函数之前就会调用这个batchedUpdates,造成的后果,就是由React控制的事件处理过程setState不会同步更新this.state。
注意: setState的“异步”并不是说内部由异步代码实现,其实本身执行的过程和代码都是同步的,只是合成事件和钩子函数的调用顺序在更新之前,导致在合成事件和钩子函数中没法立马拿到更新后的值,形式了所谓的“异步”,当然可以通过第二个参数 setState(partialState, callback) 中的callback拿到更新后的结果。
详细请看 深入 setState 机制
yygmind, zhipairen, RocGit, andylidong, hellojackhui and 161 moredongkeng001, 2384830985, laoqq, chenhq123, yky138495 and 5 moredongkeng001, laoqq, chenhq123, gaoxuerong, Twisted928 and 1 moreJaneChelle, AlexZhong22c, 827652549, justsoup, SiroSong and 2 moreTwisted928 and chinawzcNathanHan1, xiaofeifei112, yanzhang146, shenqistart, laoqq and 6 moredongkeng001, laoqq, yky138495, hmLAzzzy, Twisted928 and 2 more