美文网首页
react-reconsiler模块 - React源码解析(三

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

作者: 请叫我Pro大叔 | 来源:发表于2020-05-12 15:32 被阅读0次

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;
}

相关文章

网友评论

      本文标题:react-reconsiler模块 - React源码解析(三

      本文链接:https://www.haomeiwen.com/subject/asanyhtx.html