简体   繁体   English

create-react-app / babel / 绑定 state 和函数

[英]create-react-app / babel / binding state and functions

Thank you for your time.感谢您的时间。 I have the same issue as in this issue re parsing error - unexpected token = eslint on (originally state - which was fixed by putting in constructor, now) handleClick.我遇到了与此问题相同的问题,重新解析错误 - unexpected token = eslint on(最初是 state - 现在通过放入构造函数修复)handleClick。 New project and think this error will continue through the rest of the project.新建项目,认为这个错误会继续通过项目的rest。

class Header extends Component {
  constructor() {
    super();
    this.state = {
      activeItem: "home"
    };
  }
  
  handleClick = (event, { name }) => this.setState({ activeItem: name });
  

I am new to react, and apologise if this is a simple question.我是新手,如果这是一个简单的问题,我深表歉意。 Project was installed with create-react-app, there is no babelrc file, the eslint is as follows.项目是用create-react-app安装的,没有babelrc文件,eslint如下。

module.exports = {
  env: {
    browser: true,
    es2021: true,
    jest: true,
  },
  extends: ["plugin:react/recommended", "airbnb", "prettier"],
  parserOptions: {
    ecmaFeatures: {
      jsx: true,
    },
    ecmaVersion: 12,
    sourceType: "module",
  },
  plugins: ["react"],
  rules: {},
};

From what i have read, I have to install babel to the project, and customise the config to accept state = { activeItem: "home" };根据我的阅读,我必须将 babel 安装到项目中,并自定义配置以接受state = { activeItem: "home" }; if I want to set state / functions outside the constructor and need to use a 'basic syntax', for fixing the function, i am not sure what that basic syntax is.如果我想在构造函数之外设置 state / 函数并且需要使用“基本语法”来修复 function,我不确定基本语法是什么。 Help?, I think the course of action is to create a babelrc config file.帮助?,我认为行动的过程是创建一个 babelrc 配置文件。 but am not sure why if it is already installed with create-react-app and do not want to install a bunch of dependencies unless i have to.但不知道为什么如果它已经安装了 create-react-app 并且不想安装一堆依赖项,除非我必须这样做。

Thank you for your patience.感谢您的耐心等待。

It looks like you need to fix up your ESlint config a bit:看起来您需要稍微修复一下 ESlint 配置:

npm install eslint @babel/core @babel/eslint-parser --save-dev

and then add然后添加

parser: "@babel/eslint-parser",

to the top level of your ESLint config.到 ESLint 配置的顶层。 Alternatively you could use the default Create React App ESLint config by removing yours, which should be all set already.或者,您可以通过删除您的配置来使用默认的 Create React App ESLint 配置,该配置应该已经全部设置好了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM