I'm using webpack with React and Eslint, I have set the extends on Eslint, however, it still says 'React' is defined but never used.eslintno-unused-vars
This is my .eslintrc file:
{
"parser": "babel-eslint",
"extends": ["eslint:recommended", "plugin:react/recommended", "airbnb", "prettier"],
"parserOptions": {
"sourceType": "module",
"ecmaVersion": 2015
},
"env": {
"browser": true,
"node": true,
"jest": true,
"commonjs": true
},
"rules": {
"react/jsx-uses-react": "error",
"react/jsx-uses-vars": "error",
"react/jsx-filename-extension": [1, { "extensions": [".js", ".jsx"] }],
"linebreak-style": "off",
"no-undef": "off",
"no-debugger": "off",
"no-alert": "off",
"indent": ["error", 4],
"quotes": "off",
"no-console": "off"
}
}
React version is ^17.0.1
babel-eslint is ^10.1.0
eslint is ^7.14.0
eslint-plugin-react is ^7.21.5
Looks like it has to do with actual JSX in the file
Your config worked fine on a file that actually contains valid JSX:
// App.jsx
import React from 'react'; //
const App = () => {
return <div />; // JSX
}
export default App
But if no JSX is found in the file it throws the error you described:
// App.jsx
import React from 'react'; // 'React' is defined but never used
const App = () => {
return 1; // no JSX
}
export default App
Related
I am using class component and want to initialize state inside constructor, but eslint gives parsing error unexpected token = for state initialization and when initialize it inside constructor error goes away, what eslint rule should I use to turn this rule off?
class Component extends React.Component {
state = {
name: ''
};
}
and my eslint config is:
{
"env": {
"browser": true,
"es2021": true
},
"extends": [
"plugin:react/recommended",
"airbnb",
"airbnb/hooks",
"plugin:prettier/recommended"
],
"parserOptions": {
"ecmaFeatures": {
"jsx": true
},
"ecmaVersion": 12,
"sourceType": "module"
},
"plugins": ["react"],
"rules": {
"import/no-named-as-default": 0,
"react/prop-types": "off",
"react/jsx-props-no-spreading": "off"
}
}
in react 17 is not necessarily use
import React from 'react';
but if i don't have it, so eslint gave me error
'React' must be in scope when using JSX react/react-in-jsx-scope
any idea how modify .eslintrc.js
module.exports = {
parser: "babel-eslint",
env: {
browser: true,
node: true,
es6: true,
jest: true,
},
extends: [
"eslint:recommended",
"plugin:react/recommended",
"plugin:jsx-a11y/recommended"
],
plugins: [
"react",
"react-hooks",
"jsx-a11y",
],
rules: {
strict: 0,
"react-hooks/rules-of-hooks": "error",
"react-hooks/exhaustive-deps": "warn"
},
settings: {
react: {
version: "detect"
}
}
}
for react 17?
Thank's a lot
You can read about it in React docs.
If you are using eslint-plugin-react, the react/jsx-uses-react and react/react-in-jsx-scope rules are no longer necessary and can be turned off or removed.
{
// ...
"rules": {
// ...
"react/jsx-uses-react": "off",
"react/react-in-jsx-scope": "off"
}
}
react-in-jsx-scope on github.
To make it work, you should add those rules to your eslint config, see Extending or replacing the default ESLint config for Create-React-App specifics, every framework should have related section in their docs.
You need to add plugin:react/jsx-runtime to extends in the .eslintrc.js file.
like is:
module.exports = {
extends: [
'plugin:react/recommended',
'airbnb',
'plugin:react/jsx-runtime',
]
}
Refer here
When I run the linter command, the below configuration works.
But when I have my react dev server running it compiles with warnings - "#typescript-eslint/no-unused-vars".
How can I remove the warnings?
Here is my .eslintrc.js configuration.
module.exports = {
parser: "#typescript-eslint/parser",
parserOptions: {
ecmaVersion: 2020,
sourceType: "module",
ecmaFeatures: {
jsx: true
}
},
settings: {
react: {
version: "detect"
}
},
extends: [
"plugin:react/recommended",
"plugin:#typescript-eslint/recommended",
"prettier/#typescript-eslint",
"plugin:prettier/recommended"
],
rules: {
"#typescript-eslint/explicit-function-return-type": "off",
"#typescript-eslint/interface-name-prefix": "off",
"#typescript-eslint/no-unused-vars": "off",
"#typescript-eslint/no-explicit-any": "off"
}
};
You need to disable no-unused-vars beforehand, as mentioned in the docs.
{
// note you must disable the base rule as it can report incorrect errors
"no-unused-vars": "off",
"#typescript-eslint/no-unused-vars": ["error"]
}
Try replacing "#typescript-eslint/no-unused-vars": "off" with "#typescript-eslint/no-unused-vars": 0,
Trying to call a redux action creator inside a useEffect hook the following warning-
React Hook useEffect has a missing dependency: 'getPlanQuotes'. Either include it or remove the dependency array react-hooks/exhaustive-deps
This is the useEffect hook-
const { getPlanQuotes } = props;
useEffect(() => {
getPlanQuotes();
}, []);
So I tried disabling it using // eslint-disable-next-line react-hooks/exhaustive-deps. Like this-
useEffect(() => {
getPlanQuotes();
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
But it still throws the warning on the console without the react-hooks/exhaustive-deps being specified
And also the editor throws the following error-
.eslintrc config-
{
"parser": "babel-eslint",
"extends": ["eslint:recommended", "plugin:react/recommended", "prettier"],
"env": {
"jest": true,
"browser": true,
"node": true,
"es6": true
},
"plugins": ["json", "prettier"],
"rules": {
"prettier/prettier": ["error"],
"no-console": "off"
},
"parserOptions": {
"ecmaFeatures": {
"jsx": true
},
"rules": {
"no-underscore-dangle": [
"error",
{
"allow": ["_id", "b_codes_id"]
}
],
"react/prop-types": [1]
},
"settings": {
"import/resolver": "meteor"
},
"globals": {
"_": true,
"CSSModule": true,
"Streamy": true,
"ReactClass": true,
"SyntheticKeyboardEvent": true
}
}
}
It was a problem with the .eslintrc configuration as #DrewReese suspected. The plugins array was missing react-hooks and the rules object was missing react-hooks rules.
So, the final configuration is as follows-
{
"parser": "babel-eslint",
"extends": ["eslint:recommended", "plugin:react/recommended", "prettier"],
"env": {
"jest": true,
"browser": true,
"node": true,
"es6": true
},
"plugins": ["json", "prettier", "react-hooks"], //added "react-hooks" here
"rules": {
"prettier/prettier": ["error"],
"no-console": "off"
},
"parserOptions": {
"ecmaFeatures": {
"jsx": true
},
"rules": {
"no-underscore-dangle": [
"error",
{
"allow": ["_id", "b_codes_id"]
}
],
"react/prop-types": [1],
"react-hooks/rules-of-hooks": "error", // added "react-hooks/rules-of-hooks"
"react-hooks/exhaustive-deps": "warn" // added "react-hooks/exhaustive-deps"
},
"settings": {
"import/resolver": "meteor"
},
"globals": {
"_": true,
"CSSModule": true,
"Streamy": true,
"ReactClass": true,
"SyntheticKeyboardEvent": true
}
}
}
I am trying to set an eslint rule for methods in class that are never used. Like in the following react component I have a method unUsedMethod which is never used, but eslint does not show an error for it.
class Sample extends Component {
unUsedMethod() {
console.log('I am never used');
}
render() {
return 'Hello!';
}
}
My eslint file looks like this
{
"parser": "babel-eslint",
"env": {
"browser": true,
"es6": true,
"node": true
},
"extends": ["eslint:recommended", "plugin:react/recommended"],
"parserOptions": {
"ecmaFeatures": {
"experimentalObjectRestSpread": true,
"jsx": true,
"sourceType": "module",
"allowImportExportEverywhere": false,
"codeFrame": false
},
"sourceType": "module"
},
"plugins": [
"react"
],
"rules": {
"indent": [2, 4, {"SwitchCase": 1, "ObjectExpression": "first"}],
"linebreak-style": [
"error",
"unix"
],
"semi": [
"error",
"always"
],
"react/display-name": 0,
"react/prop-types": 0, // Temporary
"react/no-unescaped-entities": 0,
"no-trailing-spaces": 1
}
}
This plugin does what you are asking for. However a word of caution.
https://www.npmjs.com/package/eslint-plugin-no-unused-react-component-methods
Currently it is impossible to simply have a parser that would check for unused component properties, because component properties can be called dynamically.
For example:
class Sample extends Component {
// Plugin falsely flags this as unused.
unUsedMethod() {
console.log('I am used dynamically');
}
render() {
// No way to parse dynamic function calls reliably
this['unUsedMethod']();
return 'Hello!';
}
}
It also wouldn't work with react-onclickoutside package, as it requires a function to be attached to the component, that is called by wrapping the component in a HOC. https://www.npmjs.com/package/react-onclickoutside
Still the plugin helped me find a few unused functions, so it is worth a try in my opinion.
First install the package: npm i eslint-plugin-no-unused-react-component-methods --save-dev
Add "no-unused-react-component-methods" to your eslint configuration in the plugins section:
{
"plugins": [
"no-unused-react-component-methods"
],
}
And add into the rules section
{
"rules": {
"no-unused-react-component-methods/no-unused-react-component-methods": 2,
}
}
So your config would look like:
{
"parser": "babel-eslint",
"env": {
"browser": true,
"es6": true,
"node": true
},
"extends": ["eslint:recommended", "plugin:react/recommended"],
"parserOptions": {
"ecmaFeatures": {
"experimentalObjectRestSpread": true,
"jsx": true,
"sourceType": "module",
"allowImportExportEverywhere": false,
"codeFrame": false
},
"sourceType": "module"
},
"plugins": [
"react",
"no-unused-react-component-methods"
],
"rules": {
"no-unused-react-component-methods/no-unused-react-component-methods": 2,
"indent": [2, 4, { "SwitchCase": 1, "ObjectExpression": "first" }],
"linebreak-style": ["error", "unix"],
"semi": ["error", "always"],
"react/display-name": 0,
"react/prop-types": 0, // Temporary
"react/no-unescaped-entities": 0,
"no-trailing-spaces": 1
}
}
Now it should highlight any seemingly unused functions! Let me know if it works or not.