react fiber 架构渲染分析

react第一次渲染

数据结构

function FiberNode(tag, pendingProps, key, mode) {
// Instance
this.tag = tag;
this.key = key;
this.elementType = null;
this.type = null;
this.stateNode = null;
// 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.contextDependencies = null;
this.mode = mode;
this.effectTag = NoEffect;
this.nextEffect = null;
this.firstEffect = null;
this.lastEffect = null;
this.expirationTime = NoWork;
this.childExpirationTime = NoWork;
this.alternate = null;
if (enableProfilerTimer) {
Object.preventExtension().
this.actualDuration = Number.NaN;
this.actualStartTime = Number.NaN;
this.selfBaseDuration = Number.NaN;
this.treeBaseDuration = Number.NaN;
this.actualDuration = 0;
this.actualStartTime = -1;
this.selfBaseDuration = 0;
this.treeBaseDuration = 0;
}
}

reactDOM.render

legacyRenderSubtreeIntoContainer

unbatchedUpdates

root.render

updateContainer(children, root, null, work._onCommit);

updateContainerAtExpirationTime

scheduleRootUpdate

scheduleWork

requestWork

performSyncWork

performWork

performWorkOnRoot

renderRoot

workLoop

performUnitOfWork

判断next 是否存在 如果存在就return 出去继续深度遍历它beginWork,如果不存在next说明没有子组件了要走 completeUnitOfWork

beginWork

这里开始渲染每一个单位组件
1.hooks
有全局变量ReactCurrentDispatcher$1 === ReactCurrentDispatcher;
当workInProgress.tag为2的时候 此组件为hooks组件对应为IndeterminateComponent,执行mountIndeterminateComponent()方法,进入方法后调用renderWithHooks()

function renderWithHooks(current, workInProgress, Component, props, refOrContext, nextRenderExpirationTime) {
   
  renderExpirationTime = nextRenderExpirationTime;
  currentlyRenderingFiber$1 = workInProgress;
  nextCurrentHook = current !== null ? current.memoizedState : null;//如果是第一次渲染这里为空值
  {
   
    if (nextCurrentHook !== null) {
   
      ReactCurrentDispatcher$1.current = HooksDispatcherOnUpdateInDEV;//更新的时候走这里nextCurrentHook 不为空
    } else if (hookTypesDev !== null) {
   
      ReactCurrentDispatcher$1.current = HooksDispatcherOnMountWithHookTypesInDEV;
    } else {
   
      ReactCurrentDispatcher$1.current = HooksDispatcherOnMountInDEV;//第一次渲染走这里
    }
  }
  var children = Component(props, refOrContext);//这里执行函数组件然后执行useState方法
  return children;
}
function useState(initialState) {
   
      var dispatcher = resolveDispatcher(
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值