10

I know I can disable the file or disable the line, but I want to do it globally for now so I don't have to write that in every time I want to use a useEffect as a componentDidMount().

I have tried:

{
  "plugins": [
    // ...
    "react-hooks"
  ],
  "rules": {
    // ...
    "react-hooks/rules-of-hooks": "off"/0, // tried each
    "react-hooks/exhaustive-deps": "off"/0 // tried each
  },
  "overrides": [
        {
            "files": ["**/*.js"],
            "rules": {
                "react-hooks/rules-of-hooks": "off"/0, // tried each
                "react-hooks/exhaustive-deps": "off"/0 // tried each
            }
        }
    ]   
}
1
  • Have you figured it out yet? I'm struggling to enforce the rule too. I tried putting it directly in package.json and in a separate .eslitrc file. Tried all your options, with no luck whatsoever. ) Commented Nov 9, 2020 at 18:10

4 Answers 4

11

I had this same "issue" using useEffect as componentDidMount(). You can ignore that warning adding a file .eslintrc.json in the root of your project

Inside goes something like this (This worked for me):

{
"overrides": [
    {
        "files": ["**/*.js"],
        "rules": {
            "react-hooks/exhaustive-deps": "off"
        }
    }
]
}
Sign up to request clarification or add additional context in comments.

2 Comments

For me this disables other warnings, too
@HannesSchneidermayer I found the same thing. I got it working without disabling any other warnings and posted my solution below at stackoverflow.com/a/73911630/2103602, just in case it's still useful to you.
9

For my CRA (create-react-app) application, creating one of the following (equivalent) .eslintrc files at the project root did the trick for me:

.eslintrc.yml:

extends:
  - react-app
rules:
  react-hooks/exhaustive-deps: off

.eslintrc.json:

{
  "extends": [ "react-app" ],
  "rules": {
    "react-hooks/exhaustive-deps": "off"
  }
}

I found that if I didn't include the extends clause, then I disabled the react-hooks/exhaustive-deps warnings successfully, but I also lost many other warnings as well, which was unacceptable.

This solution is documented (somewhat awkwardly and incompletely) in the CRA docs.

Comments

2

We have been looking for a way to disable this rule across the project, because people trying to fix it keep introducing infinite loop bugs, often ones that are good at staying hidden until the right combination of variables comes along.

The rule basically makes an aggressive assumption that no design or thought at all has gone into the function passed to useEffect(). If there's an if/else block that calculates one dependency from the other if it's absent, handling its job correctly, blindly adding both variables to the dependencies array is very likely to trigger an infinite loop.

We can always tell everyone to add eslint-ignore lines all across the project but this is the only rule we have trouble turning off. (This particular project has the ESLint configuration inside package.json since it uses react-scripts, and we're trying to turn it off in the "rules" block by setting it to "off". I don't know if it's also a problem with .eslintrc files.)

1 Comment

I think this rule is very silly and weird
0

you have to use file extension jsx if your useEffect is in jsx file so the overrides will become as follow

{
"overrides": [
    {
        "files": ["**/*.jsx"],
        "rules": {
            "react-hooks/exhaustive-deps": "off"
        }
    }
]
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.