Skip to main content

05-2-performUnitOfWork

performUnitOfWork 作用

创建下一个 Fiber 节点,并赋值给 workInProgress,同时把 workInProgress 与已创建的 Fiber 节点连接起来构成 Fiber 树。

  function performUnitOfWork(unitOfWork) {
// The current, flushed, state of this fiber is the alternate. Ideally
// nothing should rely on this, but relying on it here means that we don't
// need an additional field on the work in progress.
var current = unitOfWork.alternate;
setCurrentFiber(unitOfWork);
var next;

if ((unitOfWork.mode & ProfileMode) !== NoMode) {
startProfilerTimer(unitOfWork);
//对当前节点进行协调,如果存在子节点,则返回子节点的引用
next = beginWork$1(current, unitOfWork, subtreeRenderLanes);
stopProfilerTimerIfRunningAndRecordDelta(unitOfWork, true);
} else {
next = beginWork$1(current, unitOfWork, subtreeRenderLanes);
}

resetCurrentFiber();
unitOfWork.memoizedProps = unitOfWork.pendingProps;

//如果无子节点,则代表当前的child链表已经遍历完
if (next === null) {
// If this doesn't spawn new work, complete the current work.
//此函数内部会帮我们找到下一个可执行的节点
completeUnitOfWork(unitOfWork);
} else {
workInProgress = next;
}

ReactCurrentOwner$2.current = null;
}