错误ESlint与类React,例如:' state'没有定义。 (无为undef)

时间:2017-07-25 19:20:57

标签: reactjs eslint eslint-config-airbnb

我实际上正在处理一个项目,当我在MacBook上克隆我的项目时,在shell yarn之后安装包,而atom .用于打开我的ATOM。 ESLint得到了一个" bug"。

例如,对于此代码:

/*
 * Package Import
 */
 import React, { Component } from 'react';
 import PropTypes from 'prop-types';
 import { Link } from 'react-router-dom';
 import classNames from 'classnames';


/*
 * Code
 */
class UserDropdown extends Component {
  /*
   * PropTypes
   */
   static propTypes = {
     ... // Code 
   }


  /*
   * State
   */
   state = {
     ... // Code
   };

  /*
   * Actions
   */
   onLogOut = () => {
     ... // Code
   };

ESLint告诉我:

Error   ESLint  'propTypes' is not defined. (no-undef)  22:10
Error   ESLint  'state' is not defined. (no-undef)  32:3
Error   ESLint  'onLogOut' is not defined. (no-undef)   52:3

我还在使用"babel-plugin-transform-class-properties": "^6.24.1", 我在我的brunch-config中声明它:

我的brunch-config.coffee

  plugins:
    babel:
      presets: ['latest', 'react']
      plugins: [
        'transform-class-properties'
        'transform-object-rest-spread'
      ]

我的.eslintrc

{
  "extends": "airbnb",
  "parser": "babel-eslint",
  "env": {
    "browser": true
  },
  "rules": {
    "brace-style": ["error", "stroustrup"],
    "no-param-reassign": ["error", { "props": false }],
    "no-mixed-operators": ["error", { "allowSamePrecedence": true }],
    "jsx-a11y/no-static-element-interactions": "off",
    "react/jsx-filename-extension": "off",
    "react/forbid-prop-types": "off",
    "react/no-unescaped-entities": "off",
    "linebreak-style": "off"
  },
  "settings": {
    "import/resolver": {
      "node": {
        "paths": ["app/"]
      }
    }
  }
}

我的Packages.json

"devDependencies": {
"auto-reload-brunch": "^2.7.1",
"autoprefixer": "^6.7.7",
"babel-brunch": "^6.1.1",
"babel-eslint": "^7.2.3",
"babel-plugin-transform-class-properties": "^6.24.1",
"babel-plugin-transform-object-rest-spread": "^6.23.0",
"babel-preset-react": "^6.24.1",
"babel-register": "^6.24.1",
"babel-resolver": "^1.1.0",
"brunch": "^2.10.9",
"chai": "^3.5.0",
"enzyme": "^2.8.2",
"eslint": "^3.19.0",
"eslint-config-airbnb": "^14.1.0",
"eslint-import-resolver-node": "^0.3.0",
"eslint-plugin-import": "^2.2.0",
"eslint-plugin-jsx-a11y": "^4.0.0",
"eslint-plugin-react": "^6.10.3",

我在2台电脑上工作,而在Linux上,它很好。 那么,有人有想法吗?如果您需要更多代码来理解,我可以发送它。谢谢你老兄!

5 个答案:

答案 0 :(得分:3)

我遇到了同样的问题,因为我安装了ESLint 4。检查你的package.json并确保它不像"eslint": "^3.19.0 || ^4.3.0"。我正在使用create-react-app,但它与ESLint 4不兼容:https://github.com/facebookincubator/create-react-app/issues/2604

答案 1 :(得分:2)

首先你必须在你的.eslintrc文件中添加babel-eslint作为dev依赖项添加:

"parser": "babel-eslint"

答案 2 :(得分:1)

升级eslint

npm i -D eslint@latest

答案 3 :(得分:0)

我通过卸载节点,纱线和npm解决了我的问题。 我使用了最新版本的节点(实际上是8.2.1,对于npm和纱线来说是相同的......)

所以在安装Node LTS(6.11.1)之后,我对ESLint没有任何问题,很好地工作。

编辑:重新安装纱线(0.27.5)后,这个问题又回到了这里:思考:

答案 4 :(得分:0)

我使用以下依赖项对其进行了修复:

package.json:

"dependencies": {
"react": "16.8.3",
"react-dom": "16.8.3",
"react-scripts": "2.1.8"
}

我做到了:

yarn
npm install webpack@4.28.3
yarn start

对我有用!