【2021.04】React 理念 - 图1

Reconciler(协调器)

由递归变成可中断的循环过程

  1. /** @noinline */
  2. function workLoopConcurrent() {
  3. // Perform work until Scheduler asks us to yield
  4. while (workInProgress !== null && !shouldYield()) {
  5. workInProgress = performUnitOfWork(workInProgress);
  6. }
  7. }

更新流程

【2021.04】React 理念 - 图2
Scheduler 和 Reconciler 在以下情况可被中断:

  • 有其他更高优先级任务需要更新
  • 当前帧没有剩余时间

    Fiber

    Fiber 树结构
    【2021.04】React 理念 - 图3

    1. function FiberNode(
    2. tag: WorkTag,
    3. pendingProps: mixed,
    4. key: null | string,
    5. mode: TypeOfMode,
    6. ) {
    7. // 作为静态数据结构的属性
    8. this.tag = tag;
    9. this.key = key;
    10. this.elementType = null;
    11. this.type = null;
    12. this.stateNode = null;
    13. // 用于连接其他Fiber节点形成Fiber树
    14. this.return = null;
    15. this.child = null;
    16. this.sibling = null;
    17. this.index = 0;
    18. this.ref = null;
    19. // 作为动态的工作单元的属性
    20. this.pendingProps = pendingProps;
    21. this.memoizedProps = null;
    22. this.updateQueue = null;
    23. this.memoizedState = null;
    24. this.dependencies = null;
    25. this.mode = mode;
    26. this.effectTag = NoEffect;
    27. this.nextEffect = null;
    28. this.firstEffect = null;
    29. this.lastEffect = null;
    30. // 调度优先级相关
    31. this.lanes = NoLanes;
    32. this.childLanes = NoLanes;
    33. // 指向该fiber在另一次更新时对应的fiber
    34. this.alternate = null;
    35. }

    双缓存树

    【2021.04】React 理念 - 图4

    参考资料

  1. React 技术揭秘