React Hook中ReactCurrentDispatcher.current
值跟踪。
// 初始化
function performSyncWorkOnRoot(root) {
if (workInProgress !== null) {
const prevExecutionContext = executionContext;
executionContext |= RenderContext;
// ReactCurrentDispatcher.current = ContextOnlyDispatcher;
// prevDispatcher = ContextOnlyDispatcher
const prevDispatcher = pushDispatcher(root);
// ...
// ReactCurrentDispatcher.current = ContextOnlyDispatcher;
popDispatcher(prevDispatcher);
}
}
// `beginWork`阶段,进入Function初始化
export function renderWithHooks(
current: Fiber | null,
workInProgress: Fiber,
Component: any,
props: any,
secondArg: any,
nextRenderExpirationTime: ExpirationTime,
): any {
// ...
ReactCurrentDispatcher.current =
current === null || current.memoizedState === null
? HooksDispatcherOnMount
: HooksDispatcherOnUpdate;
// ...
do {
// ...
ReactCurrentDispatcher.current = __DEV__
? HooksDispatcherOnRerenderInDEV
: HooksDispatcherOnRerender;
children = Component(props, secondArg);
} while (workInProgress.expirationTime === renderExpirationTime);
}
// We can assume the previous dispatcher is always this one, since we set it
// at the beginning of the render phase and there's no re-entrancy.
ReactCurrentDispatcher.current = ContextOnlyDispatcher;
}
网友评论