0

I am trying to get react-hooks rules for eslint, but it doesn't seem to be working with my setup (VSCode, CRA, airbnb, tslint, prettier).

Here's my .eslintrc.json before it breaks:

{
  "env": {
    "browser": true,
    "es6": true,
    "jest": true
  },
  "extends": ["plugin:react/recommended", "airbnb", "prettier"],
  "globals": {
    "Atomics": "readonly",
    "SharedArrayBuffer": "readonly"
  },
  "parser": "@typescript-eslint/parser",
  "parserOptions": {
    "ecmaFeatures": {
      "jsx": true
    },
    "ecmaVersion": 2020,
    "sourceType": "module"
  },
  "plugins": ["react", "@typescript-eslint"],
  "rules": {
    "react/jsx-filename-extension": [1, { "extensions": [".jsx", ".tsx"] }]
  }
}

Here are my devDependencies:

  "devDependencies": {
    "@testing-library/jest-dom": "^5.5.0",
    "@testing-library/react": "^10.0.3",
    "@testing-library/user-event": "^10.1.0",
    "@types/jest": "^25.2.1",
    "@types/node": "^13.13.4",
    "@types/react": "^16.9.34",
    "@types/react-dom": "^16.9.7",
    "@typescript-eslint/eslint-plugin": "^2.30.0",
    "@typescript-eslint/parser": "^2.30.0",
    "eslint": "^6.8.0",
    "eslint-config-airbnb": "^18.1.0",
    "eslint-config-prettier": "^6.11.0",
    "eslint-plugin-import": "^2.20.2",
    "eslint-plugin-jsx-a11y": "^6.2.3",
    "eslint-plugin-prettier": "^3.1.3",
    "eslint-plugin-react": "^7.19.0",
    "eslint-plugin-react-hooks": "^3.0.0",
    "prettier": "^2.0.5",
    "typescript": "~3.8.3"
  },

Then I tried adding the following to extends in .eslintrc:

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

I also tried placing "plugin:react-hooks/recommended" in a difference place in the array. No matter what I do, it will break all other lint rules, including its own.

I also tried adding the following to plugins and rules:

"plugins": ["react", "react-hooks", "@typescript-eslint"],
"rules": {
  // ...
  "react-hooks/rules-of-hooks": "error",
  "react-hooks/exhaustive-deps": "warn"
}

And of course, the test code for checking the lint rules:

  const [a, b] = React.useState("Hey");

  React.useEffect(() => {
    React.useMemo(() => {}, []);
    if (a) b("Yeppp");
    if (props.what) console.log(props.what);
    exampleMethod();
  }, []);

  const exampleMethod = () => {};

EDIT: Forgot to mention I also tried to use airbnb/hooks in extends as well:

"extends": ["plugin:react/recommended", "airbnb", "airbnb/hooks", "prettier"],
benard-kong
  • 1,733
  • 2
  • 8
  • 15

1 Answers1

1

I found the solution so I am answering my own question.

The eslint-plugin-react-hooks docs says if using Create React App, to make sure NOT to add the module directly in package.json.
Hence, the solution is to remove eslint-plugin-react-hooks from devDependencies.

benard-kong
  • 1,733
  • 2
  • 8
  • 15