【问题标题】:What does this do in the React codebase?这在 React 代码库中有什么作用?
【发布时间】:2017-06-29 11:27:03
【问题描述】:

我在 React 代码库中看到以下内容:

export type TypeOfWork = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10;

它在做什么?

来源:https://github.com/facebook/react/blob/master/src/shared/ReactTypeOfWork.js

【问题讨论】:

    标签: javascript reactjs


    【解决方案1】:

    这是流式或打字稿语法。

    它定义了一个自定义类型,只能是0到10之间的数字。

    export type TypeOfWork = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10;
    let x: TypeOfWork = 0;
    
    x = 12; // <-- compiler would throw an error here
    

    在这种情况下,枚举可能会更好(仅适用于打字稿)

    https://www.typescriptlang.org/docs/handbook/enums.html

    export const enum TypeOfWork {
      IndeterminateComponent = 0, // Before we know whether it is functional or class
      FunctionalComponent = 1,
      ClassComponent = 2,
      HostRoot = 3, // Root of a host tree. Could be nested inside another node.
      HostPortal = 4, // A subtree. Could be an entry point to a different renderer.
      HostComponent = 5,
      HostText = 6,
      CoroutineComponent = 7,
      CoroutineHandlerPhase = 8,
      YieldComponent = 9,
      Fragment = 10
    };
    

    正如您在compiled view 中看到的那样,这会更好地缩小

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-11-07
      • 1970-01-01
      • 2011-04-13
      • 2015-07-19
      • 2011-05-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多