React native 将ES7静态PropType与React Native一起使用
当我使用React Native default packager启动我的项目时,我遇到以下错误:React native 将ES7静态PropType与React Native一起使用,react-native,ecmascript-next,React Native,Ecmascript Next,当我使用React Native default packager启动我的项目时,我遇到以下错误:此行出现意外标记: static propTypes = { /// ... }; 我在GitHub上查看了React原生问题,但没有找到解决方案 有什么建议吗 尝试将您的propTypes附加到类中: var MyClass extends React.Component { .... } MyClass.propTypes = { .... /* enter proptypes her
此行出现意外标记
:
static propTypes = {
/// ...
};
我在GitHub上查看了React原生问题,但没有找到解决方案
有什么建议吗 尝试将您的propTypes附加到类中:
var MyClass extends React.Component {
....
}
MyClass.propTypes = {
.... /* enter proptypes here */
}
React Native packager使用Babel传输ES6和ES7,但不是所有功能。启用列表为。在您的情况下,在RN packager中默认情况下不启用。您可以在packager之前使用Babel编译代码,也可以在packager设置中启用它。有关更多信息,请参见此。在@Fomahaut-answer之后,我一直在查看Facebook的GitHub存储库,发现了以下问题:
- 在项目的根目录中创建.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
}
安装(npm i--save dev babel-preset-stage-0
)并将其添加到.babelrc
文件的预设部分,例如:
{“预设”:[“反应”、“es2015”、“巴别塔预设阶段-0”]
根据,您需要为Babel 6的类属性安装插件
从Babel 6开始,您现在需要该插件
支持类属性
步骤:
运行此命令:npm安装巴别塔插件转换类属性
将此添加到您的.babelrc:“插件”:[“转换类属性”]
(注意,它是一个插件,不是转换;所以不要将它放在“预设”列表中。)
为我工作。看看这是否有帮助:
$npm安装babel插件转换装饰器
导航到//node\u modules/react native/packager/react packager/.babelrc
将“transform decorators”
添加到此列表:
{
“保留线”:正确,
“紧凑”:没错,
“评论”:错,
“插件”:[
“语法异步函数”,
“语法类属性”,
“语法尾随函数逗号”,
“转换类属性”,
“transform-es2015-arrow-functions”,
“transform-es2015-block-scoping”,
“transform-es2015-classes”,
“transform-es2015-computed-properties”,
“transform-es2015-constants”,
“transform-es2015-destructuring”,
[“transform-es2015-modules-commonjs”,{“strict”:false,“allowTopLevelThis”:true}],
“transform-es2015-parameters”,
“transform-es2015-shorthand-properties”,
“transform-es2015-spread”,
“transform-es2015-template-literals”,
“转换流条类型”,
“转换对象分配”,
“变换对象静止排列”,
“转换显示名称”,
“transform react jsx”,
“变换再生器”,
“transform-es2015-for-of”,
-->“**transform decorators**”这不适用于react native的较新版本,可能是因为切换到了Babel 6。不确定“白名单”或这个特定的用例,但定义了“插件”的顶级.babelrc文件对我来说在RN 0.19+上非常有用。@skypecakes”_(ツ)_/''您的示例是针对ES6的。OP请求ES7的帮助。这可能就是否决票的原因。