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" ] } }
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.
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.
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!
ESLint is a JavaScript linter (static analysis tool) that offers full support for ES6, JSX, and other modern tools via plugins.
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
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" }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With