gpt4 book ai didi

reactjs - 解析错误: Unexpected token const

转载 作者:行者123 更新时间:2023-12-03 14:00:04 25 4
gpt4 key购买 nike

我目前正在设置一个 Meteor 应用程序,并且正在使用 eslint 和 babel,但我收到以下代码片段的错误:

const Navigation = props => (
const classes = props.classes;

return (
<div className={classes.root}>
</div>
)
);

错误:

2:4 - Parsing error: Unexpected token const

我重新创建了我的 eslint 配置 here 。我的 .babelrc 配置如下:

{
"presets": ["env", "react"]
}

最佳答案

这是因为您使用的是 arrow function简洁正文并且需要 () 内的表达式而不是语句。要使用语句,您需要使用 {} 而不是 () 来使用 block 体

像这样:

const Navigation = props => {
const classes = props.classes;

return (
<div className={classes.root}>
</div>
)
};

根据 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.

关于reactjs - 解析错误: Unexpected token const,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46283308/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com