源码分析之React中的FiberRoot节点属性介绍

概览

FiberRootNode是一个构造函数,用于创建FiberRoot即Fiber根节点,是整个React应用的起点,包含了整个应用的状态信息。

源码解析

FiberRootNode的源码实现如下:

js 复制代码
function FiberRootNode(
  containerInfo,
  tag,
  hydrate,
  identifierPrefix,
  onUncaughtError,
  onCaughtError,
  onRecoverableError,
  onDefaultTransitionIndicator,
  formState
) {
  this.tag = 1; // FiberRoot的类型 1表示并发模式ConcurrentRoot ;0 表示传统模式 LegacyRoot
  this.containerInfo = containerInfo; // 宿主容器,即React应用挂载的DOM节点
  this.pingCache = // 用于缓存ping操作的结果,与Suspense相关
    this.current = // 指向当前页面上显示的Fiber树(current树)的根节点,即HostRootFiber
    this.pendingChildren = null; // 等待渲染的子元素,通常在更新时使用
  this.timeoutHandle = -1; // 用于存储setTimeout返回的id,用以清除
  this.callbackNode = // 调度器返回的任务节点,用于取消任务
    this.next = // 指向下一个FiberRoot,在多个React应用时可能使用
    this.pendingContext = // 等待处理的上下文
    this.context = // 当前上下文
    this.cancelPendingCommit = // 用于取消尚未提交的更新
      null;
  this.callbackPriority = 0; // 回调优先级,表示当前正在执行的任务的优先级
  this.expirationTimes = createLaneMap(-1); // 一个数组,存储每个lane对应的过期时间
  this.entangledLanes =// 表示哪些lane是一起的,它们的更新必须一起处理
    this.shellSuspendCounter = //与Suspense相关的计数器,用于跟踪挂起的shell主内容数量
    this.errorRecoveryDisabledLanes =// 禁止错误恢复的lane
    this.expiredLanes =//已过期的lane,需要立即同步更新
    this.warmLanes = // 预热lane,用于并发更新,表示已经准备就绪的更新
    this.pingedLanes = // 被ping的lane,与suspense相关,表示可以重新尝试更新的lane
    this.suspendedLanes = // 被挂起的lane,表示暂停的更新
    this.pendingLanes = //等待处理的lane,表示所有待处理的更新
      0;
  this.entanglements = createLaneMap(0); // 一个数组,表示每个lane与其他lane的纠缠关系
  this.hiddenUpdates = createLaneMap(null); // 一个数组,存储每个lane上隐藏的更新
  this.identifierPrefix = identifierPrefix;// 用于生成React唯一ID的前缀
  this.onUncaughtError = onUncaughtError; // 未捕获错误的回调函数
  this.onCaughtError = onCaughtError; // 已捕获错误的回调函数
  this.onRecoverableError = onRecoverableError; // 可恢复错误的回调函数
  this.pooledCache = null;// 缓存的池,用于缓存资源
  this.pooledCacheLanes = 0; // 使用缓存池的lane
  this.formState = formState; //与表单状态相关,用于React的状态管理
  this.incompleteTransitions = new Map(); // 未完成的过渡的映射,用于并发模式下的过渡更新
}

FiberRoot

FiberRootReactDOM.createRoot时都会被创建,它负责管理整个应用的更新任务,包括任务的调度、优先级、挂起和恢复等。

  • FiberRoot工作流程
js 复制代码
// 创建 FiberRoot
const root = createFiberRoot(
  containerInfo,
  tag,
  hydrate,
  identifierPrefix,
  onRecoverableError
);

// 调度更新
scheduleUpdateOnFiber(root, fiber, lane);

// 渲染完成
commitRoot(root);

总结

FiberRootNode 是 React 渲染系统的核心,它管理着:

  1. 渲染调度 :通过 Lane 模型管理更新优先级
  2. 状态管理:维护应用状态和更新队列
  3. 错误处理:提供完整的错误处理机制
  4. 缓存系统:优化渲染性能
  5. 并发控制:支持可中断渲染和时间切片

关键特性:

  • 双缓存架构 :通过 currentalternate 树平滑切换
  • 优先级调度Lane 模型支持精细的优先级控制
  • 错误恢复:完善的错误边界和恢复机制
  • 资源管理:缓存、预加载等优化手段
  • 过渡处理:支持 React 18+ 的过渡更新

理解 FiberRootNode 的结构和工作原理对于深入理解 React 内部机制、性能优化和高级特性开发至关重要。

相关推荐
掘金安东尼23 分钟前
用 CSS 打造完美的饼图
前端·css
掘金安东尼8 小时前
纯 CSS 实现弹性文字效果
前端·css
牛奶9 小时前
Vue 基础理论 & API 使用
前端·vue.js·面试
牛奶9 小时前
Vue 底层原理 & 新特性
前端·vue.js·面试
anOnion9 小时前
构建无障碍组件之Radio group pattern
前端·html·交互设计
pe7er9 小时前
状态提升:前端开发中的状态管理的设计思想
前端·vue.js·react.js
SoaringHeart10 小时前
Flutter调试组件:打印任意组件尺寸位置信息 NRenderBox
前端·flutter
晚风予星11 小时前
Ant Design Token Lens 迎来了全面升级!支持在 .tsx 或 .ts 文件中直接使用 Design Token
前端·react.js·visual studio code
sunny_11 小时前
⚡️ vite-plugin-oxc:从 Babel 到 Oxc,我为 Vite 写了一个高性能编译插件
前端·webpack·架构
GIS之路11 小时前
ArcPy 开发环境搭建
前端