上篇讲了React的工作流程,这篇详细的讲一下render过程中的beginWork函数,也就是深度优先创建Fiber树中"递"的过程。
先复习一下,workLoop调用performUnitOfWork函数开始深度优先处理创建FIber节点,如下:
function performUnitOfWork(fiber: FiberNode) {
const next = beginWork(fiber, wipRootRenderLane);
fiber.memoizedProps = fiber.pendingProps;
if (next === null) {
completeUnitOfWork(fiber);
} else {
workInProgress = next;
}
}
beginWork函数传入当前已经创建好的Fiber节点,返回创建/复用的第一个子FIber节点,也就是wip.child
在renderRoot中,在开始创建整棵FIber树之前,会先调用prepareRefreshStack创建/复用一个新的HostRootFiber。
从此节点开始,通过beginWork函数,比较其现有子节点和新的ReactElement,判断Fiber节点是否可复用。 注意调用一次beginWork函数,仅处理,生成一个FIber节点的子节点。
BeginWork
beginWork函数定义在 react-reconcile/beginwork.ts 中,此函数的作用就是,根据不同的节点类型,调用不同节点类型的处理函数,其实现如下:
// 递归中的递阶段
export const beginWork = (wip: FiberNode) => {
// 比较,返回子fiberNode
switch (wip.tag) {
case HostRoot:
return updateHostRoot(wip, renderLane);
case HostComponent:
return updateHostComponent(wip);
case HostText:
return null;
case FunctionComponent:
return updateFunctionComponent(wip, wip.type, renderLane);
case Fragment:
return updateFragment(wip);
case ContextProvider:
return updateContextProvider(wip, renderLane);
case SuspenseComponent:
return updateSuspenseComponent(wip);
case OffscreenComponent:
return updateOffscreenComponent(wip);
case LazyComponent:
return mountLazyComponent(wip, renderLane);
case MemoComponent:
return updateMemoComponent(wip, renderLane);
default:
if (__DEV__) {
console.warn('beginWork未实现的类型');
}
break;
}
return null;
};
你或许会疑惑,wip.tag这个属性是什么时候设置的
wip.tag代表FIber节点的类型,其ts定义为WorkTag类型,定义在react-reconciler/workTag.ts中。其中定义了React支持的所有节点类型,如下:
export type WorkTag =
| typeof FunctionComponent
| typeof HostRoot
| typeof HostComponent
| typeof HostText
| typeof Fragment
| typeof ContextProvider
| typeof SuspenseComponent
| typeof OffscreenComponent
| typeof LazyComponent
| typeof MemoComponent;
export const FunctionComponent = 0;
export const HostRoot = 3;
export const HostComponent = 5;
export const HostText = 6;
export const Fragment = 7;
export const ContextProvider = 8;
export const SuspenseComponent = 13;
export const OffscreenComponent = 14;
export const LazyComponent = 16;
export const MemoComponent = 15;
在调用createRoot函数时,会调用createContainer函数,其内部创建FIberRootNode和HostRootFiber节点,其实现如下:
/** 创建应用根节点FiberRootNode 以及第一个HostRoot节点 hostRootFiber */
const createContainer = (container: Container) => {
// 第一个hostRoot节点
const hostRootFiber = new FiberNode(HostRoot, {}, null);
// 创建整个应用的根节点: FiberRootNode
const root = new FiberRootNode(container, hostRootFiber);
// 创建一个hostRootFiber的更新队列 (updateQueue) 存放的是新的root element
hostRootFiber.updateQueue = new UpdateQueue<ReactElement>();
return root;
};
可以看到,创建hostRootFIber时,传入构造函数的第一个参数,就是HostRoot对应的tag
在第一次渲染(挂载mount)时,renderRoot调用prepareRefreshStack创建wipHostRoot节点。
由于是通过createWorkInProgress函数创建,其内部实现会复用tag类型,所以新的wipHostRoot也
会包含HostRoot类型的tag。
Flags
Fiber节点上包含属性flags,其类型为Flags,具体定义在react-reconciler/flags.ts 如下:
export type Flags = number;
export const NoFlags = 0b0000000;
export const Placement = 0b0000001;
export const Update = 0b0000010;
export const ChildDeletion = 0b0000100;
其代表对节点的一些操作,其中
NoFlags: 代表无任何标记,默认状态
Placement: 代表放置元素,创建元素
Update:代表更新元素,可以复用元素,需要更新属性
ChildDeletion: 代表删除元素
这些标记用不同的位数二进制标志,每个二进制位代表一个flag类型,类似于lane模型的设计。
这样的好处是很直观的能获取Fiber节点包含哪些属性。
updateHostRoot - 更新Fiber树根结点
我们先来看对于根节点的reconcile,其实现如下:
/** 处理HostRoot节点的比较 */
function updateHostRoot(wip: FiberNode, renderLane: Lane): FiberNode {
/** 获取updateQueue */
const updateQueue = wip.updateQueue;
/** 这里hostRoot的update由updateContainer放入,其对应的action就是其element */
const { memorizedState: newChildren } = updateQueue.process(renderLane);
/** 协调其子节点 */
reconcileChildren(wip, newChildren);
/** 返回下一个待处理节点 即wip.child */
return wip.child;
}
在上面updateContianer实现中,我们在scheduleUpdateOnRoot之前,向HostRootFiber的updateQueue中添加了要渲染的App组件对应的ReactElement,并且在createWorkInProgress函数复用currentHostFiber时也复用了updateQueue。
所以我们在处理HostRoot节点时,可以直接通过updateQueue.process获取当前要渲染的ReactElement对象(即HostRoot的children Element)。
拿到新的children Element 将其传入reconcileChildren函数进行协调,reconcileChildren会根据传入的Fiber节点和其新的Children Element对象协调创建/复用出其所有的children Fiber 并且返回第一个子节点,即wip.child。
reconcileChildren - 判断是否需要添加副作用
这里涉及到React的一个优化策略。我们知道,React采用深度优先的方式创建Fiber节点,Fiber节点中包含stateNode指针,用来保存Fiber节点对应的真实DOM对象,方便Commit节点的挂载,删除,更新操作。
Fiber节点的创建在BeginWork(也就是递)的阶段完成,但是其中stateNode真实DOM元素的创建在归的过程也就是completeWork中完成。 在CompleteWork中,对于首次挂载(Mount)的节点,会创建其真实DOM元素,并且完成DOM元素与其父元素的连接工作。
所以,对于组件首次挂载阶段,在commit时,已经有一颗完整的真实DOM树已经被创建完成了,此时只需要直接挂载此节点即可,不需要再往下继续遍历了。如图所示:

首次创建时,从HostRootFiber开始深度优先创建Fiber树,并且在completeWork阶段,完成真实DOM对象的创建与连接,当"归"到HostRootFiber时,此时HostRootFiber的的子树已经包含一棵完整的真实DOM树了,此时直接挂载HostRootFiber即可。
为了实现此优化,React仅在非首次挂载的节点上打标签, 对于首次挂载的节点,其一定包含一个可以完成对其的挂载祖先节点。
判断是否首次挂载的方式是,查看当前节点的alternate节点是否存在,如果存在就不是第一次挂载,如果是则为第一次挂载。如图,右侧的HostRootFiber是prepareRefreshStack创建,属于非首次挂载,其下面的节点都是首次挂载。

实现逻辑在 reconcileChildren方法
/
2万+



被折叠的 条评论
为什么被折叠?



