• react源码中的fiber架构


    先看一下FiberNode在源码中的样子

    FiberNode

    // packages/react-reconciler/src/ReactFiber.old.js
    function FiberNode(
      tag: WorkTag,  pendingProps: mixed,  key: null | string,  mode: TypeOfMode,
    ) {
       
      // 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.dependencies = null;
    
      this.mode = mode;
    
      // Effects
      this.flags = NoFlags;
      this.nextEffect = null;
    
      this.firstEffect = null;
      this.lastEffect = null;
    
      this.lanes = NoLanes;
      this.childLanes = NoLanes;
    
      this.alternate = null;
    
      if (enableProfilerTimer) {
       
        // Note: The following is done to avoid a v8 performance cliff.
        //
        // Initializing the fields below to smis and later updating them with
        // double values will cause Fibers to end up having separate shapes.
        // This behavior/bug has something to do with Object.preventExtension().
        // Fortunately this only impacts DEV builds.
        // Unfortunately it makes React unusably slow for some applications.
        // To work around this, initialize the fields below with doubles.
        //
        // Learn more about this here:
        // https://github.com/facebook/react/issues/14365
        // https://bugs.chromium.org/p/v8/issues/detail?id=8538
        this.actualDuration = Number.NaN;
        this.actualStartTime = Number.NaN;
        this.selfBaseDuration = Number.NaN;
        this.treeBaseDuration = Number.NaN;
    
        // It's okay to replace the initial doubles with smis after initialization.
        // This won't trigger the performance cliff mentioned above,
        // and it simplifies other profiler code (including DevTools).
        this.actualDuration = 0;
        this.actualStartTime = -1;
        this.selfBaseDuration = 0;
        this.treeBaseDuration = 0;
      }
    
      if (__DEV__) {
       
        // This isn't directly used but is handy for debugging internals:
        ...
      }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 我们看FiberNode这个构造函数里面只是赋值,我们再找一下链路上的Fiber,我们发现在函数createFiber的返回值类型里面出现了Fiber类型,所以
    // packages/react-reconciler/src/ReactInternalTypes.js
    export type Fiber = {
       |
      // These first fields are conceptually members of an Instance. This used to
      // be split into a separate type and intersected with the other Fiber fields,
      // but until Flow fixes its intersection bugs, we've merged them into a
      // single type.
    
      // An Instance is shared between all versions of a component. We can easily
      // break this out into a separate object to avoid copying so much to the
      // alternate versions of the tree. We put this on a single object for now to
      // minimize the number of objects created during the initial render.
    
      // dom节点的相关信息
      tag: WorkTag,// 组件的类型
      key: null | string, // 唯一值
      elementType: any,// 元素类型
    
      // 判定fiber节点的类型,用于diff
      type: any,
    
      // 真实 dom 节点
      stateNode: any,
    
      // Conceptual aliases
      // parent : Instance -> return The parent happens to be the same as the
      // return fiber since we've merged the fiber and instance.
      // Remaining fields belong to Fiber
    
      // fiber 链表树
      return: Fiber | null, // 父 fiber
      child: Fiber | null, // 第一个子 fiber
      sibling: Fiber | null, // 下一个兄弟 fiber
      index: number, // 在父 fiber 下面的子 fiber 中的下标
    
      // The ref last used to attach this node.
      // I'll avoid adding an owner field for prod and model that as functions.
      ref:
        | null
        | (((handle: mixed) => void) & {
       _stringRef: ?string, ...})
        | RefObject,
    
      // 计算 state 和 props 渲染
      pendingProps: any, // 本次渲染需要使用的 props
      memoizedProps: any, // 上次渲染使用的 props
      updateQueue: mixed, // 用于状态更新、回调函数、DOM更新的队列
      memoizedState: any
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
  • 相关阅读:
    代码随想录训练营二刷第十六天 | 104.二叉树的最大深度 559.n叉树的最大深度 111.二叉树的最小深度 222.完全二叉树的节点个数
    学习笔记-.net安全
    python脚本打包apk-上传到内测平台-企业微信通知
    计算机毕业设计Java旅游官网(源码+mysql数据库+系统+lw文档)
    C++ getline():从文件中读取一行字符串
    uni.scanCode不支持h5扫码(用拍照或者获取相册识别二维码和条码)
    RabbitMQ简介
    C语言经典面试题目(十二)
    三、c++代码中的安全风险-open
    数据结构之<RBTree >
  • 原文地址:https://blog.csdn.net/weixin_59558923/article/details/127401334