Search code examples
typescriptreact-hookseslintcreate-react-apptesting-library

Eslint not catching react-hooks/exhaustive-deps violations


I'm trying to get warnings for missing dependencies in React hooks, but am not getting warned. For instance, in the following example, I would like to be notified by eslint that the following useEffect hook is missing dependencies for state2 and state3.

useEffect(() => {
    console.log(state2)
    console.log(state3)
}, [state1])

Here is my .eslintrc.js file:

module.exports = {
  'env': {
    'browser': true,
    'es2021': true,
  },
  'extends': [
    'plugin:testing-library/react',
    'google',
    "prettier",
  ],
  'parser': '@typescript-eslint/parser',
  'parserOptions': {
    'ecmaFeatures': {
      'jsx': true,
    },
    'ecmaVersion': 'latest',
    'sourceType': 'module',
  },
  'plugins': [
    'react',
    '@typescript-eslint',
  ],
  'rules': {
    "require-jsdoc" : 0
  },
};

And some portions of my package.json:

{
  "dependencies": {
    "@emotion/react": "^11.9.3",
    "@emotion/styled": "^11.9.3",
    "@types/jest": "^27.5.2",
    "@types/node": "^16.11.44",
    "@types/react": "^18.0.15",
    "@types/react-dom": "^18.0.6",
    "firebase": "^9.9.0",
    "react": "^18.2.0",
    "react-scripts": "5.0.1",
    "typescript": "^4.7.4",
  },
  "eslintConfig": {
    "extends": [
      "react-app",
      "react-app/jest"
    ]
  },
  "devDependencies": {
    "@testing-library/jest-dom": "^5.16.4",
    "@testing-library/react": "^13.3.0",
    "@testing-library/user-event": "^13.5.0",
    "@typescript-eslint/eslint-plugin": "^5.30.6",
    "@typescript-eslint/parser": "^5.30.6",
    "eslint": "^8.19.0",
    "eslint-config-google": "^0.14.0",
    "eslint-config-prettier": "^8.5.0",
    "eslint-plugin-react": "^7.30.1",
    "eslint-plugin-react-hooks": "^4.6.0",
    "prettier": "2.7.1"
  }
}

I do get warned for things like unused variables, but not for this particular violation. This project was created using create-react-app. Are there any other places that I should be looking to try to solve this issue?


Solution

  • I was able to solve this with eslint-plugin-react-hooks

    Install it with npm: npm install eslint-plugin-react-hooks --save-dev

    Add it to .eslintrc:

    "extends": [
        "plugin:react-hooks/recommended"
      ],
    "plugins": [
        "react-hooks"
      ],