LouisTsang-jk.github.io

Fiber

看卡颂文章的学习笔记,此处链接| iamkasong

含义

Fiber Node

每个 Fiber Node 对应 React Element

基本结构

主要分三类

function FiberNode(
  tag: WorkTag,
  pendingProps: mixed,
  key: null | string,
  mode: TypeOfMode,
) {
  // 作为静态数据结构的属性
  this.tag = tag;
  this.key = key;
  this.elementType = null;
  this.type = null;
  this.stateNode = null;

  // 用于连接其他Fiber节点形成Fiber树
  this.return = null;
  this.child = null;
  this.sibling = null;
  this.index = 0;

  this.ref = null;

  // 作为动态的工作单元的属性
  this.pendingProps = pendingProps;
  this.memoizedProps = null;
  this.updateQueue = null;
  this.memoizedState = null;
  this.dependencies = null;

  this.mode = mode;

  this.effectTag = NoEffect;
  this.nextEffect = null;

  this.firstEffect = null;
  this.lastEffect = null;

  // 调度优先级相关
  this.lanes = NoLanes;
  this.childLanes = NoLanes;

  // 指向该fiber在另一次更新时对应的fiber
  this.alternate = null;
}

Fiber Reconciler

更新机制(双缓存)

在内存中绘制当前帧动画,绘制完毕后直接用当前帧替换上一帧画面,避免白屏闪烁的情况出现

React 使用“双缓存”来完成 Fiber 树的构建替换(对应着 DOM 树的创建与更新)。

双缓存树

render

该阶段异步

beginWork

rootFiber开始深度优先遍历,创建传入的FiberNode的子节点,当遍历到叶节点。

effectTag: render 阶段后通知Renderer需要执行的 DOM 操作(PlacementAndUpdate、Deletion)

completeWork

commit

该阶段同步

里面又分三个阶段:

这里每个阶段本质都是遍历effectList

参考

iamkasong | Fiber 的起源