将ES7静态propType与React-Native一起使用

Jea*_*ent 6 react-native ecmascript-next

当我使用React-Native默认打包器启动我的项目时,我有这个错误:Unexpected token在这一行:

static propTypes = {
   /// ...
};
Run Code Online (Sandbox Code Playgroud)

我看了一下GitHub上的React-Native问题,但我找不到解决方案.

有什么建议吗?

Fom*_*aut 7

React-Native包装程序使用Babel进行传输ES6和ES7,但不是所有功能.启用列表在这里.在您的情况下,RN packager中默认情况下不启用class-props.您可以使用Babel在打包程序之前编译代码,或者只在打包程序设置中启用它.有关更多信息,请参阅此官方文档.


Jea*_*ent 3

在 @Fomahaut 回答之后,我继续查看 Facebook 的 GitHub 存储库并发现了这个问题: https: //github.com/facebook/react-native/issues/2182

  • 在项目根目录创建.babelrc文件
  • 向 Babel 添加更多规则

例子:

    {
      "retainLines": true,
      "compact": true,
      "comments": false,
      "whitelist": [
        "es6.arrowFunctions",
        "es6.blockScoping",
        "es6.classes",
        "es6.constants",
        "es6.destructuring",
        "es6.forOf",
        "es6.modules",
        "es6.parameters",
        "es6.properties.computed",
        "es6.properties.shorthand",
        "es6.spread",
        "es6.tailCall",
        "es6.templateLiterals",
        "es6.regex.unicode",
        "es6.regex.sticky",
        "es7.asyncFunctions",
        "es7.classProperties",
        "es7.comprehensions",
        "es7.decorators",
        "es7.exponentiationOperator",
        "es7.exportExtensions",
        "es7.functionBind",
        "es7.objectRestSpread",
        "es7.trailingFunctionCommas",
        "regenerator",
        "flow",
        "react",
        "react.displayName"
        ],
      "sourceMaps": false
    }
Run Code Online (Sandbox Code Playgroud)