reactjs - 解析错误: Unexpected token const

标签 reactjs meteor ecmascript-6 babeljs eslint

我目前正在设置一个 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/

相关文章:

javascript - Meteor 应用程序运行时出现 pm2 fatal error : CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory

javascript - 以编程方式调用对象中的所有函数? (JavaScript)

Javascript (ES6) 模块 : Is it possible to export a variable with a dynamic name?

javascript - 如何在下拉列表中选择一项来显示 react 数组中的数据?

javascript - React 组件在 JSON 对象上执行 foreach 返回空

Meteor - 如何在 Helper 中获取当前访问者的 IP 地址

node.js - Meteor SSR "Error: Can' t 渲染未定义”

javascript - react 多个回调不更新本地状态

javascript - 将 ParticleJS 与 Gatsby 结合使用

javascript - mobx-react 创建可观察存储