React短文系列 遍历fiber树 App的创建

从createRoot到App节点的创建

createRoot->renderRootSync->prepareFreshStack->workLoopSync->HostRoot:updateHostRoot->reconcileChildren创建节点:FiberRootNode->HostRootFiber->App

  1. createRoot->renderRootSync->prepareFreshStack(root,lanes) 这一步创建出了FiberRootNodeHostRootFiber节点。

    a. createRoot创建了FiberRootNode

    b. prepareFreshStack根据FiberRootNode创建HostRootFiber

    c. HostRootFiberHostRootFiberNode,对应<div id="root"></div>

    d. rootFiberRootNode,root.currentHostRootFiber,复制一份赋值给rootWorkInProgress是第一个workInProgress

  2. workLoopSync->beginWork->HostRoot:updateHostRoot(current,workInProgress,)->reconcileChildren 马上进入到了workLoopSync创建出App

    a. currentwip是上面的HostRootFiber

    b. 进入到reconcileChildren,创建HostRootFiber的子节点App

js 复制代码
createRoot-renderRootSync:
  // root是fiberRootNode, tag=1
  prepareFreshStack(root, lanes)  //这一步创建tag=3,HostRoot,HostRootFiber,rootElement 的wip

  do {
    try {
      workLoopSync();  //这一步创建tag=3的子fiber,因为进入到beginWork-updateHostRoot-reconcileChildren
      break;
    } catch (thrownValue) {
      handleError(root, thrownValue);
    }
  } while (true);

prepareFreshStack:
  var rootWorkInProgress = createWorkInProgress(root.current, null);
  return rootWorkInProgress; // tag=3
createWorkInProgress:
  workInProgress.alternate = current; //创建tag=3的cur的wip
  current.alternate = workInProgress;

workLoopSync-beginWork:
case HostRoot:
      return updateHostRoot(current, workInProgress, renderLanes);
updateHostRoot:
    reconcileChildren(current, workInProgress, nextChildren, renderLanes);
reconcileChildren:
  if (current === null) {
    // 2.然后遍历到App节点进入这里创建App节点的子节点
    workInProgress.child = mountChildFibers(workInProgress, null, nextChildren, renderLanes);
  } else {
    // 1.首先HostRootFiber进入这里创建子节点App节点
    workInProgress.child = reconcileChildFibers(workInProgress, current.child, nextChildren, renderLanes);
  }
}

一些特点

  1. fiberRootNode tag=1,HostRootNode tag=3,App tag=2
  2. current节点current树当前遍历到的节点,workInProgress节点workInProgress树当前遍历到的节点。
  3. current节点wip节点通过alternate相互指向。第一次渲染,HostRootFibercurrent节点不是空的,但是App节点current节点是空的null

通常不画null,如图左所示。

  1. 创建完wip树后切换current树,图3
相关推荐
RadiumAg26 分钟前
记一道有趣的面试题
前端·javascript
yangzhi_emo30 分钟前
ES6笔记2
开发语言·前端·javascript
yanlele1 小时前
我用爬虫抓取了 25 年 5 月掘金热门面试文章
前端·javascript·面试
中微子2 小时前
React状态管理最佳实践
前端
烛阴2 小时前
void 0 的奥秘:解锁 JavaScript 中 undefined 的正确打开方式
前端·javascript
中微子2 小时前
JavaScript 事件与 React 合成事件完全指南:从入门到精通
前端
Hexene...2 小时前
【前端Vue】如何实现echarts图表根据父元素宽度自适应大小
前端·vue.js·echarts
天天扭码3 小时前
《很全面的前端面试题》——HTML篇
前端·面试·html
xw53 小时前
我犯了错,我于是为我的uni-app项目引入环境标志
前端·uni-app
!win !3 小时前
被老板怼后,我为uni-app项目引入环境标志
前端·小程序·uni-app