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
JavaScript
x
14
14
1
import React, { Component } from 'react';
2
import Header from './header.js';
3
4
export default class App extends Component {
5
render() {
6
return (
7
<div>
8
<Header />
9
{this.props.children}
10
</div>
11
);
12
}
13
}
14
Linter Errors:
JavaScript
1
4
1
/my_project/src/components/app.js
2
1:8 error 'React' is defined but never used no-unused-vars
3
2:8 error 'Header' is defined but never used no-unused-vars
4
Here is my .eslintrc.json
file:
JavaScript
1
37
37
1
{
2
"env": {
3
"browser": true,
4
"es6": true
5
},
6
"extends": "eslint:recommended",
7
"parserOptions": {
8
"ecmaFeatures": {
9
"experimentalObjectRestSpread": true,
10
"jsx": true
11
},
12
"sourceType": "module"
13
},
14
"plugins": [
15
"react"
16
],
17
"rules": {
18
"react/jsx-filename-extension": [1, { "extensions": [".js", ".jsx"] }],
19
"indent": [
20
"error",
21
2
22
],
23
"linebreak-style": [
24
"error",
25
"unix"
26
],
27
"quotes": [
28
"error",
29
"single"
30
],
31
"semi": [
32
"error",
33
"always"
34
]
35
}
36
}
37
Advertisement
Answer
First, install the following module npm install --save-dev eslint-plugin-react
.
Then, in your .eslintrc.json
, under extends
, include the following plugin:
JavaScript
1
4
1
'extends': [
2
'plugin:react/recommended'
3
]
4