解析错误:意外的令牌const [英] Parsing error: Unexpected token const
问题描述
我当前正在设置一个流星应用程序,并且正在使用eslint和babel,但是以下代码段出现以下错误:
I am currently setting up a meteor application and I am using eslint and babel, but I get the following error for the following code snippet:
const Navigation = props => (
const classes = props.classes;
return (
<div className={classes.root}>
</div>
)
);
错误:
2:4 - Parsing error: Unexpected token const
我已经重新我eslint配置此处. 我的.babelrc配置如下:
I have recreated my eslint config here. My .babelrc config is the following:
{
"presets": ["env", "react"]
}
推荐答案
It's because you are using the concise body
of an arrow function and that expects the expression inside ()
and not a statement. To use a statement you need to use block body
by using {}
instead of ()
.
赞:
const Navigation = props => {
const classes = props.classes;
return (
<div className={classes.root}>
</div>
)
};
根据 MDN文档 :
As per MDN Doc:
箭头功能可以具有简洁主体"或通常的块" 身体".
Arrow functions can have either a "concise body" or the usual "block body".
在简洁的正文中,仅需要一个表达式,而隐式 返回附件.在块体中,必须使用显式返回 声明.
In a concise body, only an expression is needed, and an implicit return is attached. In a block body, you must use an explicit return statement.
这篇关于解析错误:意外的令牌const的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!