版权声明:王为仁 https://blog.csdn.net/wangweiren_get/article/details/84206588
面试问题之react 的 setState是否异步
看看源码里面的setState方法构造
Component.prototype.setState = function (partialState, callback) {
!(typeof partialState === 'object' || typeof partialState === 'function'
|| partialState == null) ? invariant(false,
`setState(...): takes an object of state variables to update or a
function which returns an object of state variables.`) : void 0;
this.updater.enqueueSetState(this, partialState, callback, 'setState');
};
我们可以看到setState可以有两个参数一个是partialState(部分state), 一个是回调
函数。然后就进入了 this.updater 的 enqueueSetStateh函数里面, 我们先看一些这个 this
-.updater对象,再看 enqueueSetState函数的代码,看是不是能发现点什么。
this.updater 相关代码
Component 构造函数, 创建的组件的时候会传入updater,我们先看没有传入updater的情况
组件解析情况
未完待续。。。