You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
/** * @param {object|function} partialState Next partial state or function to * produce next partial state to be merged with current state. * @param {?function} callback Called after state is updated. * @final * @protected */Component.prototype.setState=function(partialState,callback){invariant(typeofpartialState==="object"||typeofpartialState==="function"||partialState==null,"setState(...): takes an object of state variables to update or a "+"function which returns an object of state variables.");this.updater.enqueueSetState(this,partialState,callback,"setState");};
/** * Forces an update. This should only be invoked when it is known with * certainty that we are **not** in a DOM transaction. * * You may want to call this when you know that some deeper aspect of the * component's state has changed but `setState` was not called. * * This will not invoke `shouldComponentUpdate`, but it will invoke * `componentWillUpdate` and `componentDidUpdate`. * * @param {?function} callback Called after update is complete. * @final * @protected */Component.prototype.forceUpdate=function(callback){this.updater.enqueueForceUpdate(this,callback,"forceUpdate");};
setState
setState
的定义在packages/react/src/ReactBaseClasses.js
:可以看到内部调用了
enqueueSetState
:传入的
inst
即调用this.setState
时传进来的this
,也就是classComponent
实例。通过
getInstance
方法来获取目标对象的_reactInternalFiber
属性。下面依次拿到
currentTime
和expirationTime
,然后通过createUpdate
来创建update
对象。最后
update
入队、进入任务调度。接着来看下
forceUpdate
。forceUpdate
定义同样在
packages/react/src/ReactBaseClasses.js
:内部调用了
enqueueForceUpdate
方法:与
enqueueSetState()
方法的流程类似,唯一不同的是多了个手动修改属性tag
的值:可以看到
createUpdate()
方法中,初始化的tag
值是UpdateState
:因此要改成
ForceUpdate
,以便React
进行Update
优先级排序。The text was updated successfully, but these errors were encountered: