React16源码: React中的updateHostRoot的源码实现

news/2024/7/14 20:43:54 标签: react.js, 前端, 前端框架

HostRoot 的更新


1 )概述

  • HostRoot 是一个比较特殊的节点, 因为在一个react应用当中
  • 它只会有一个 HostRoot, 它对应的 Fiber 对象是我们的 RootFiber 对象
  • 重点在于它的更新过程

2 )源码

定位到 packages/react-reconciler/src/ReactFiberBeginWork.js#L612

// 这个函数的重点在: update 来自哪里, 里面是什么内容
// 最终通过 processUpdateQueue 得到了 element 里面的内容,之后以此作为children来调和
function updateHostRoot(current, workInProgress, renderExpirationTime) {
  // 跳过 context 相关
  pushHostRootContext(workInProgress);
  const updateQueue = workInProgress.updateQueue;
  invariant(
    updateQueue !== null,
    'If the root does not have an updateQueue, we should have already ' +
      'bailed out. This error is likely caused by a bug in React. Please ' +
      'file an issue.',
  );
  // 获取一系列数据
  const nextProps = workInProgress.pendingProps;
  const prevState = workInProgress.memoizedState;
  // 对于 HostRoot 一开始是没有 state,也就是 `prevState.element`, 在第一次渲染的时候,prevState 是 null,在ReactDOM.render中创建了一个update
  // 经过 processUpdateQueue 这次更新后,它会拿到一个 {element} 对象作为 state
  const prevChildren = prevState !== null ? prevState.element : null;
  // 得到创建的update传递的element
  processUpdateQueue(
    workInProgress,
    updateQueue,
    nextProps,
    null,
    renderExpirationTime,
  );
  const nextState = workInProgress.memoizedState;
  // Caution: React DevTools currently depends on this property
  // being called "element".
  const nextChildren = nextState.element;
  if (nextChildren === prevChildren) {
    // If the state is the same as before, that's a bailout because we had
    // no work that expires at this time.
    resetHydrationState(); // 服务端渲染,复用dom节点相关内容
    // 跳出更新过程,不需要更新
    // 对 RootFiber来说,大部分情况下,只在 ReactDOM.render 的时候有更新,其他时候都不需要更新
    // 一般都是在App内更新,不会在RootFiber节点创建更新
    return bailoutOnAlreadyFinishedWork(
      current,
      workInProgress,
      renderExpirationTime,
    );
  }
  const root: FiberRoot = workInProgress.stateNode;
  // 跳过 hydrate 相关
  if (
    (current === null || current.child === null) &&
    root.hydrate &&
    enterHydrationState(workInProgress)
  ) {
    // If we don't have any current children this might be the first pass.
    // We always try to hydrate. If this isn't a hydration pass there won't
    // be any children to hydrate which is effectively the same thing as
    // not hydrating.

    // This is a bit of a hack. We track the host root as a placement to
    // know that we're currently in a mounting state. That way isMounted
    // works as expected. We must reset this before committing.
    // TODO: Delete this when we delete isMounted and findDOMNode.
    workInProgress.effectTag |= Placement;

    // Ensure that children mount into this root without tracking
    // side-effects. This ensures that we don't store Placement effects on
    // nodes that will be hydrated.
    // 在 current === null || current.child === null 这种情况下,都是第一次渲染
    workInProgress.child = mountChildFibers(
      workInProgress,
      null,
      nextChildren,
      renderExpirationTime,
    );
  } else {
    // Otherwise reset hydration state in case we aborted and resumed another
    // root.
    // 不是第一次渲染
    reconcileChildren(
      current,
      workInProgress,
      nextChildren,
      renderExpirationTime,
    );
    resetHydrationState();
  }
  return workInProgress.child;
}
  • HostRoot 创建更新的过程就是在 ReactFiberReconciler.js 中的调用 ReactDOM.render 的过程
  • 定位到 scheduleRootUpdate 位置在 packages/react-reconciler/src/ReactFiberReconciler.js#L110
    function scheduleRootUpdate(
      current: Fiber,
      element: ReactNodeList,
      expirationTime: ExpirationTime,
      callback: ?Function,
    ) {
      // ... 省略
      const update = createUpdate(expirationTime);
      update.payload = {element};
      // ... 省略
      return expirationTime;
    }
    
    • 它这里创建一个 update, 并挂在 update.payload 是 {element}
    • 这个 element 就是传给 ReactDOM.render 的第一个参数
    • 这个 update 对象没有后续指定类型
    • 这和调用 setState 在组件内创建更新效果是类似的
    • 所以,update.payload 就相当于 state
    • 对于 HostRoot 来说, 它的state只有一个属性,就是element
    • 就是 ReactDOM.render 的第一个参数

http://www.niftyadmin.cn/n/5337617.html

相关文章

Unity中ShaderGraph下获取主灯

文章目录 前言一、ShaderGraph获取主灯1、创建ShaderGraph2、创建一个自定义方法(Custom Function)节点3、新建两个 Vector3 类型的输出变量4、选择自定义节点程序体为 string 类型5、编写程序体6、我们输出主光方向看看效果7、我们输出主光颜色看看效果…

采集B站up主视频信息

一、网页信息(示例网址:https://space.bilibili.com/3493110839511225/video) 二、查看响应数据 三、查看数据包内容 四、相关代码(代码内容未进行翻页爬取) # Time: 2024/1/19 16:42 # Author: 马龙强 # File: 采集B…

vue 实现 全部页全部选

这个是页面 <!--表格--><a-table class"tableBox":columns"columns" :dataSource"data" :loading"loadingTable" :pagination"pagination" change"changePage" showSizeChange"showSizeChange"…

零基础学Python(3)— 注释、代码缩进和编码规范

前言&#xff1a;Hello大家好&#xff0c;我是小哥谈。在使用Python语言进行编程的时候&#xff0c;需要遵循一定的规范标准。本节课就带大家了解下Python语言在注释、缩进和编码方面的规范!~&#x1f308; 目录 &#x1f680;1.注释 &#x1f680;2.代码缩进 &#x1f68…

Deepin_Ubuntu_查看树形目录结构(tree)

Linux系统&#xff08;Deepin、Ubuntu&#xff09;中&#xff0c;可以使用tree命令来查看树形目录结构&#xff0c;下面是一些示例&#xff1a; 查看当前目录的树形结构&#xff1a; tree查看指定目录的树形结构&#xff0c;例如/etc/X11/fonts目录&#xff1a; tree /etc/X…

[LeetCode]-动态规划-4

前言 记录 LeetCode 刷题时遇到的动态规划相关题目&#xff0c;第四篇 1504.统计全1子矩形 枚举算法&#xff1a;首先对整个矩阵生成一个 row 数组&#xff0c;其中 row[i][j] 表示从 mat[i][j] 开始往左连续的 1 的个数 然后枚举的思路是&#xff0c;枚举所有的 mat[i][j]…

VIM工程的编译 / VI的快捷键记录

文章目录 VIM工程的编译 / VI的快捷键记录概述笔记工程的编译工程的编译 - 命令行vim工程的编译 - GUI版vim备注VIM的帮助文件位置VIM官方教程vim 常用快捷键启动vi时, 指定要编辑哪个文件正常模式光标的移动退出不保存 退出保存只保存不退出另存到指定文件移动到行首移动到行尾…

QT+opencv源码编译

时间记录&#xff1a;2024/1/20 一、版本介绍 QT5.12.7cmake3.22.0opencv4.5.4 二、编译步骤 &#xff08;1&#xff09;下载opencv源码&#xff0c;然后安装&#xff0c;opencv的安装即对源码的解压过程&#xff0c;解压后的文件目录如下 &#xff08;2&#xff09;openc…