This is what worked for me:
- First install the appropriate eslint plugin:
Copy $ npm i --save-dev eslint-plugin-react-hooks
- Add it into your .eslintrc together with default config:
Copy {
"plugins": [
...
"react-hooks"
],
"rules": {
...
"react-hooks/rules-of-hooks": "error",
"react-hooks/exhaustive-deps": "warn"
}
...
- Install the eslint configuration template "react-app":
Copy $ npm i --save-dev eslint-config-react-app
- In your .eslintrc extend from the newly installed configuration:
Copy {
...
"extends": "react-app"
- Make sure you have the proper peer dependencies of the new package, e.g. I had to do:
Copy $ npm i --save-dev eslint-plugin-flowtype
- Make sure your eslint package is version 5+ e.g. 5.16.0 works, higher should also work (but beware that higher eslint also requires newer nodejs).
Copy $ npm i --save-dev eslint@^5.0.0
- Restart VSCode
- Press Cmd-Shift-U to open the output terminal, switch to Eslint in the dropdown menu and check that it loaded without errors.
» npm install eslint-plugin-react-hooks
reactjs - Integration of eslint-plugin-react-hooks/recommended with eslint.config.js - Stack Overflow
eslint-plugin-react-hooks & "Flat Config" (ESLint 9)
Bug: "ESLint couldn't determine the plugin "react-hooks" uniquely." error occurs while committing file
Bug: eslint-plugin-react-hooks not flagging hook used as value
This is what worked for me:
- First install the appropriate eslint plugin:
Copy $ npm i --save-dev eslint-plugin-react-hooks
- Add it into your .eslintrc together with default config:
Copy {
"plugins": [
...
"react-hooks"
],
"rules": {
...
"react-hooks/rules-of-hooks": "error",
"react-hooks/exhaustive-deps": "warn"
}
...
- Install the eslint configuration template "react-app":
Copy $ npm i --save-dev eslint-config-react-app
- In your .eslintrc extend from the newly installed configuration:
Copy {
...
"extends": "react-app"
- Make sure you have the proper peer dependencies of the new package, e.g. I had to do:
Copy $ npm i --save-dev eslint-plugin-flowtype
- Make sure your eslint package is version 5+ e.g. 5.16.0 works, higher should also work (but beware that higher eslint also requires newer nodejs).
Copy $ npm i --save-dev eslint@^5.0.0
- Restart VSCode
- Press Cmd-Shift-U to open the output terminal, switch to Eslint in the dropdown menu and check that it loaded without errors.
Actually, when I paste your function on the App.js file created by create-react-app, there is the expected error when running the app.
Maybe your function isn't considered a React Component (your function is considered a common function). Make sure you have React on scope (import React from "react";)
When I tried the config snippet in the question by @Jonathan Kasser, I got an error: TypeError: context.getSource is not a function.
The solution is to use fixupPluginRules from @eslint/compat (you need to install this), so my eslint.config.js has a separate object in the config array:
import { fixupPluginRules } from "@eslint/compat";
// Even though eslint-plugin-react-hooks exposes configs.recommended, it is not yet compatible with the flat file config,
// because it has plugins: [ 'react-hooks' ] property, but plugins should be an object
// Once it is supported, replace with: eslintPluginReactHooks.configs.recommended,
{
plugins: {
"react": reactPlugin, // remove this if you already have another config object that adds the react plugin
"react-hooks": fixupPluginRules(eslintPluginReactHooks),
},
rules: {
...eslintPluginReactHooks.configs.recommended.rules,
},
},
Resources
- https://github.com/t3-oss/create-t3-turbo/issues/984#issuecomment-2210934687
Proof it works
I've confirmed this by having a code that should error, and saw the error:
const [first, setfirst] = useState("");
useEffect(() => {
console.log(first);
}, []);
8:6 warning React Hook useEffect has a missing dependency: 'first'. Either include it or remove the dependency array react-hooks/exhaustive-deps
[email protected] was released a few weeks ago with "stable" eslint v9 support. If you can upgrade, then see https://github.com/facebook/react/issues/28313#issuecomment-2408157792 for a sample configuration for now.
Also, follow this thread if interested in documentation refresh
Forgive me if this question is poorly worded or misinformed; I'm new to using NPM.
My team has eslint installed as a dev dependency in our project to ensure high code quality. However, we don't use the eslint-plugin-react or eslint-plugin-react-hooks packages. I'd like to install them so that I can follow the best React practices when coding, without actually making changes to the official dependencies used by all developers. Is there a way to go about this (eg, modify a personal "packages.json" that is separate from the project's but will still lint my company's project)?
» npm install eslint-plugin-react