简体   繁体   中英

EsLint misinterpreting import statements in no-unused-vars

I have my react code as normal:

import React from 'react';
import Header from '../components/Common/Header';

// ...
header: (props) => <Header {...props} />
// ...other code

I just wanted to show that all the imports are being used, but still giving false error.

I am using ESlint config no-unused-vars rule, but unfortunately it is doing this:

E:\React\sg_app\src\containers\Todo\TodoScreen.js
  1:8   error  'React' is defined but never used                 no-unused-vars
  2:10  error  'View' is defined but never used                  no-unused-vars
  2:28  error  'StatusBar' is defined but never used             no-unused-vars
  5:8   error  'TodoFilter' is defined but never used            no-unused-vars
  6:8   error  'TodoListView' is defined but never used  

My eslint config

module.exports = {
  "extends": "eslint:recommended",
  "parser": "babel-eslint",
  "env": {
    "es6": true,
    "jest": true,
  },
  "parserOptions": {
    "sourceType": "module",
  },
  "rules": {
      "semi": "warn",
      "no-unused-vars": "warn",
      "no-use-before-define": "off",
      "no-multiple-empty-lines": ["warn", {"max": 2}],
      "no-console": "warn",
      "arrow-parens": ["error", "as-needed"],
      "comma-dangle": "off",
      // "import/first": "error"
  }
};

How do i stop eslint from giving errors to the imports like React, View etc which are used as components, but mistakenly shown as unused?

Answering this for anyone's future reference I found out the fix with suggestion of @FunkeyFlo thanks for the suggestion plugin:react/recommended this npmjs.com/package/eslint-plugin-react package extend is necessary in order to make eslint work in react.

"extends": [
    "eslint:recommended",
    "plugin:react/recommended"  //this plugin is required to use eslint for react, otherwise we will get errors for 'React' import, as 'no-unused-vars'
  ],

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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