浅谈ReactFiber

1. 什么是fiber

每个ReactElement都有一个对应的fiber, 记录这个节点的各类状态, fiber是一链表的结构的串联起来。
fiber-tree.pngjavascript

2. Fiber的组成

export type Fiber = {|
 
          // Tag identifying the type of fiber.
        //区分fiber的种类
  tag: WorkTag,

          // Unique identifier of this child.
        // 像react元素中的惟一的key
  key: null | string,

          // The value of element.type which is used to preserve the identity during
          // reconciliation of this child.
            //就是creatElement的第一个值,用来在子节点reconciliation阶段的标识
  elementType: any,

          // The resolved function/class/ associated with this fiber.
        //异步组件加载resovled后种类是函数式仍是类
  type: any,

          // The local state associated with this fiber.
        //与这个fiber联系的本地状态,指向实例
  stateNode: any,

          // It is conceptually the same as the return address of a stack frame.
         // 指向 Fiber Tree 中的父节点
  return: Fiber | null,

          // Singly Linked List Tree Structure.
         // 指向第一个子节点
  child: Fiber | null,
        // 指向兄弟节点
  sibling: Fiber | null,
  index: number,

  // 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,

          // Input is the data coming into process this fiber. Arguments. Props.
          //新的即将进来的props
  pendingProps: any, // This type will be more specific once we overload the tag.
            // 如今的已经展现在UI上的props
  memoizedProps: any, // The props used to create the output.
        
          // A queue of state updates and callbacks.
        // 保存更新的状态和回调函数
  updateQueue: UpdateQueue<any> | null,

      // The state used to create the output
      // 展现在UI中的state
  memoizedState: any,

  // A linked-list of contexts that this fiber depends on
  contextDependencies: ContextDependencyList | null,

  mode: TypeOfMode,

      // Effect
    //反作用
  effectTag: SideEffectTag,

          // Singly linked list fast path to the next fiber with side-effects.
        // 单链表结构,方便遍历 Fiber Tree 上有反作用的节点
  nextEffect: Fiber | null,

  // The first and last fiber with side-effect within this subtree. This allows
  // us to reuse a slice of the linked list when we reuse the work done within
  // this fiber.
    //在子节点中的第一个和最后一个的反作用,这个能够容许咱们进行切片的复用
  firstEffect: Fiber | null,
  lastEffect: Fiber | null,

  // Represents a time in the future by which this work should be completed.
  // Does not include work found in its subtree.
  expirationTime: ExpirationTime,

  // This is used to quickly determine if a subtree has no pending changes.
  childExpirationTime: ExpirationTime,

  // This is a pooled version of a Fiber. Every fiber that gets updated will
  // eventually have a pair. There are cases when we can clean up pairs to save
  // memory if we need to.
  alternate: Fiber | null,

  // Time spent rendering this Fiber and its descendants for the current update.
  // This tells us how well the tree makes use of sCU for memoization.
  // It is reset to 0 each time we render and only updated when we don't bailout.
  // This field is only set when the enableProfilerTimer flag is enabled.
  actualDuration?: number,

  // If the Fiber is currently active in the "render" phase,
  // This marks the time at which the work began.
  // This field is only set when the enableProfilerTimer flag is enabled.
  actualStartTime?: number,

  // Duration of the most recent render time for this Fiber.
  // This value is not updated when we bailout for memoization purposes.
  // This field is only set when the enableProfilerTimer flag is enabled.
  selfBaseDuration?: number,

  // Sum of base times for all descedents of this Fiber.
  // This value bubbles up during the "complete" phase.
  // This field is only set when the enableProfilerTimer flag is enabled.
  treeBaseDuration?: number,

  // Conceptual aliases
  // workInProgress : Fiber ->  alternate The alternate used for reuse happens
  // to be the same as work in progress.
  // __DEV__ only
  _debugID?: number,
  _debugSource?: Source | null,
  _debugOwner?: Fiber | null,
  _debugIsCurrentlyTiming?: boolean,

  // Used to verify that the order of hooks does not change between renders.
  _debugHookTypes?: Array<HookType> | null,
|};
相关文章
相关标签/搜索