onClick = () => {
this.setState({
num: this.state.num + 1
})
this.setState({
num: this.state.num + 1
})
}
onClick = () => {
setTimeout(() => {
this.setState({
num: this.state.num + 1
})
this.setState({
num: this.state.num + 1
})
})
}
旧版本更新机制
- 整个过程是同步执行的,如果改为异步调用 setTimeout
- 回调函数执行时形成的 executionContext 在setTimeout 中拿不到
export function batchedUpdates<A, R>(fn: A => R, a: A): R {
const prevExecutionContext = executionContext;
executionContext |= BatchedContext;
try {
return fn(a);
} finally {
executionContext = prevExecutionContext;
if (executionContext === NoContext) {
resetRenderTimer();
flushSyncCallbacksOnlyInLegacyMode();
}
}
}
新版本更新机制
- 原理是对比 lane ,如果前后两次的 lane 相同直接 return
setState
|
|
V
enqueueSetState(inst, payload, callback)
- const fiber = getInstance(inst);
- const lane = requestUpdateLane(fiber);
- const update = createUpdate(eventTime, lane);
|
|
V
const root = enqueueUpdate(fiber, update, lane);
|
|
V
scheduleUpdateOnFiber(root, fiber, lane, eventTime);
- ensureRootIsScheduled(root, eventTime);
- - const existingCallbackNode = root.callbackNode;
- - scheduleCallback
- - performConcurrentWorkOnRoot
- - - prepareFreshStack
- - - - workInProgressRoot = root;
- - - - const rootWorkInProgress = createWorkInProgress(root.current, null);
|
|
V
setState
...
...
const existingCallbackNode = root.callbackNode;
existingCallbackNode !== null