react-reconsiler模块 - React源码解析(三

2020-05-12  本文已影响0人  请叫我Pro大叔

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;
}
上一篇 下一篇

猜你喜欢

热点阅读