React16源码: React中commitAllHostEffects内部的commitWork源码实现

commitWork


1 )概述

  • 在 react commit 阶段的 commitRoot 第二个while循环中
  • 调用了 commitAllHostEffects,这个函数不仅仅处理了新增节点,
  • 若一个节点已经存在,当它有新的内容要更新或者是它的attributes要更新
  • 这个时候,就需要调用 commitWork

2 )源码

定位到 packages/react-reconciler/src/ReactFiberCommitWork.js#L1033

function commitWork(current: Fiber | null, finishedWork: Fiber): void {
  if (!supportsMutation) {
    switch (finishedWork.tag) {
      case FunctionComponent:
      case ForwardRef:
      case MemoComponent:
      case SimpleMemoComponent: {
        commitHookEffectList(UnmountMutation, MountMutation, finishedWork);
        return;
      }
    }

    commitContainer(finishedWork);
    return;
  }

  // 首先,根据不同的tag类型来执行不同的操作
  switch (finishedWork.tag) {
    case FunctionComponent:
    case ForwardRef:
    case MemoComponent:
    case SimpleMemoComponent: {
      commitHookEffectList(UnmountMutation, MountMutation, finishedWork);
      return;
    }
    // ClassComponent 不在这里的执行范围内,所以它就return了
    case ClassComponent: {
      return;
    }
    case HostComponent: {
      const instance: Instance = finishedWork.stateNode;
      // // 如果instance不等于null,就代表 instance 对应的节点已经创建了
      if (instance != null) {
        // Commit the work prepared earlier.
        const newProps = finishedWork.memoizedProps;
        // For hydration we reuse the update path but we treat the oldProps
        // as the newProps. The updatePayload will contain the real change in
        // this case.
        const oldProps = current !== null ? current.memoizedProps : newProps;
        const type = finishedWork.type;
        // TODO: Type the updateQueue to be specific to host components.
        // 获取了一个 updatePayload 等于 finishedWork.updateQueue
        // 在 completeWork 的时候,对于 HostComponent 进行了新老 props 的一个对比
        // 这个对比的结果就是返回一个叫做 updatePayload 这么一个数组
        // 这个数组是以2为单位,就是说一个key一个value, 它的第一位是key,第二位是value,然后这样的形式一个一个往后面堆叠
        // 拿到这么一个 updatePayload 之后,我们就知道在commit阶段要去执行怎么样的一个更新操作
        const updatePayload: null | UpdatePayload = (finishedWork.updateQueue: any);
        finishedWork.updateQueue = null;
        // 存在 updatePayload 则执行 commitUpdate
        if (updatePayload !== null) {
          commitUpdate(
            instance,
            updatePayload,
            type,
            oldProps,
            newProps,
            finishedWork,
          );
        }
      }
      return;
    }
    case HostText: {
      invariant(
        finishedWork.stateNode !== null,
        'This should have a text node initialized. This error is likely ' +
          'caused by a bug in React. Please file an issue.',
      );
      const textInstance: TextInstance = finishedWork.stateNode;
      const newText: string = finishedWork.memoizedProps;
      // For hydration we reuse the update path but we treat the oldProps
      // as the newProps. The updatePayload will contain the real change in
      // this case.
      const oldText: string =
        current !== null ? current.memoizedProps : newText;
      commitTextUpdate(textInstance, oldText, newText);
      return;
    }
    case HostRoot: {
      return;
    }
    case Profiler: {
      return;
    }
    case SuspenseComponent: {
      let newState: SuspenseState | null = finishedWork.memoizedState;

      let newDidTimeout;
      let primaryChildParent = finishedWork;
      if (newState === null) {
        newDidTimeout = false;
      } else {
        newDidTimeout = true;
        primaryChildParent = finishedWork.child;
        if (newState.timedOutAt === NoWork) {
          // If the children had not already timed out, record the time.
          // This is used to compute the elapsed time during subsequent
          // attempts to render the children.
          newState.timedOutAt = requestCurrentTime();
        }
      }

      if (primaryChildParent !== null) {
        hideOrUnhideAllChildren(primaryChildParent, newDidTimeout);
      }
      return;
    }
    case IncompleteClassComponent: {
      return;
    }
    default: {
      invariant(
        false,
        'This unit of work tag should not have side-effects. This error is ' +
          'likely caused by a bug in React. Please file an issue.',
      );
    }
  }
}
  • 进入 commitUpdate

    // packages/react-dom/src/client/ReactDOMHostConfig.js#L324
    export function commitUpdate(
      domElement: Instance,
      updatePayload: Array<mixed>,
      type: string,
      oldProps: Props,
      newProps: Props,
      internalInstanceHandle: Object,
    ): void {
      // Update the props handle so that we know which props are the ones with
      // with current event handlers.
      updateFiberProps(domElement, newProps); // 将新的props挂载到新的属性上
      // Apply the diff to the DOM node.
      updateProperties(domElement, updatePayload, type, oldProps, newProps);
    }
    
    // packages/react-dom/src/client/ReactDOMComponentTree.js#L85
    export function updateFiberProps(node, props) {
      node[internalEventHandlersKey] = props;
    }
    
    // packages/react-dom/src/client/ReactDOMComponent.js#L775
    // Apply the diff.
    export function updateProperties(
      domElement: Element,
      updatePayload: Array<any>,
      tag: string,
      lastRawProps: Object,
      nextRawProps: Object,
    ): void {
      // Update checked *before* name.
      // In the middle of an update, it is possible to have multiple checked.
      // When a checked radio tries to change name, browser makes another radio's checked false.
      // 像是 radio 可以设置 value 绑定来设置
      // 对于radio标签,设置通过 value 的绑定来进行是否选中的控制, 相当于是要把value转化成哪一个radio标签,它的checked属性是等于true的这么一种情况
      // 这是为什么当初在执行 diff property 的时候,判断如果这个标签是input,它的 updatePayload 直接等于了一个空数组
      // 也就是说,即便我们后续 updatePayload 的里面没有放任何的内容,但它返回的仍然是一个空数组,仍然要执行到我们这个 updateProperties 方法里面
      // 因为最终我们要去根据一些不同的情况来更新,像radio标签,checkbox 标签 的一个 checked 相关的一些状态
      if (
        tag === 'input' &&
        nextRawProps.type === 'radio' &&
        nextRawProps.name != null
      ) {
        ReactDOMInput.updateChecked(domElement, nextRawProps);
      }
    
      const wasCustomComponentTag = isCustomComponent(tag, lastRawProps);
      const isCustomComponentTag = isCustomComponent(tag, nextRawProps);
      // Apply the diff.
      // 更新 dom 节点的 properties
      updateDOMProperties(
        domElement,
        updatePayload,
        wasCustomComponentTag,
        isCustomComponentTag,
      );
    
      // TODO: Ensure that an update gets scheduled if any of the special props
      // changed.
      // 表单标签的处理
      // 其实就是把这些 input, textarea, select 标签, 特殊的一些属性,一些 attribute 给它进行一个更新的一个过程
      switch (tag) {
        case 'input':
          // Update the wrapper around inputs *after* updating props. This has to
          // happen after `updateDOMProperties`. Otherwise HTML5 input validations
          // raise warnings and prevent the new value from being assigned.
          ReactDOMInput.updateWrapper(domElement, nextRawProps);
          break;
        case 'textarea':
          ReactDOMTextarea.updateWrapper(domElement, nextRawProps);
          break;
        case 'select':
          //