Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ESLint with React gives `no-unused-vars` errors

I've setup eslint & eslint-plugin-react.

When I run ESLint, the linter returns no-unused-vars errors for each React component.

I'm assuming it's not recognizing that I'm using JSX or React syntax. Any ideas?

Example:

app.js

import React, { Component } from 'react'; import Header from './header.js';  export default class App extends Component {   render() {     return (       <div>         <Header />         {this.props.children}       </div>     );   } } 

Linter Errors:

/my_project/src/components/app.js   1:8  error  'React' is defined but never used   no-unused-vars   2:8  error  'Header' is defined but never used  no-unused-vars 

Here is my .eslintrc.json file:

{     "env": {         "browser": true,         "es6": true     },     "extends": "eslint:recommended",     "parserOptions": {         "ecmaFeatures": {             "experimentalObjectRestSpread": true,             "jsx": true         },         "sourceType": "module"     },     "plugins": [         "react"     ],     "rules": {         "react/jsx-filename-extension": [1, { "extensions": [".js", ".jsx"] }],         "indent": [             "error",             2         ],         "linebreak-style": [             "error",             "unix"         ],         "quotes": [             "error",             "single"         ],         "semi": [             "error",             "always"         ]     } } 
like image 491
Don P Avatar asked Mar 01 '17 20:03

Don P


People also ask

Why are there no unused variables?

Variables that are declared and not used anywhere in the code are most likely an error due to incomplete refactoring. Such variables take up space in the code and can lead to confusion by readers.

Does ESLint work with react?

You can add ESLint in any of your JavaScript Code. It's not only limited to React Projects. You can use it with Vue. js, Node.

Is not defined no undef?

js error "X is not defined react/jsx-no-undef" occurs when we forget to import a function, class or a variable in our code before using it. To solve the error, make sure to import the value before using it in your code, e.g. import {myFunc} from 'my-package' . Copied!

Does ESLint work with JSX?

ESLint is a JavaScript linter (static analysis tool) that offers full support for ES6, JSX, and other modern tools via plugins.


2 Answers

First, install the following module npm install --save-dev eslint-plugin-react.

Then, in your .eslintrc.json, under extends, include the following plugin:

'extends': [     'plugin:react/recommended' ] 

Source

like image 51
edwarddamato Avatar answered Oct 13 '22 18:10

edwarddamato


To solve this only problem without adding new rules from react/recommended install eslint-plugin-react:

npm install eslint-plugin-react --save-dev 

add in .eslintrc.js:

"plugins": ["react"] 

and:

"rules": {         "react/jsx-uses-react": "error",         "react/jsx-uses-vars": "error"  } 
like image 41
Mihey Mik Avatar answered Oct 13 '22 16:10

Mihey Mik