A
A
Andrey Okhotnikov2019-05-07 16:29:45
React
Andrey Okhotnikov, 2019-05-07 16:29:45

Eslint and React hooks?

What the linter does not like in this case, I act according to the React docks, but the linter is against this approach :)
5cd1884285b77239152299.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
alexkhismatulin, 2019-05-13
@tsepen

The purpose of this rule is to useEffectclearly show how dependencies affect the actions performed in the effect. In your case, how data the call to getList in the effect affects is not obvious.
How can you solve:
1. Add the data parameter to getList, this will make the dependency explicit:

useEffect(() => {
  getList(data)
}, [data])

2. If you don't want to follow the rule, disable it in .eslintrc:
{
  "plugins": ["react-hooks"],
  // ...
  "rules": {
    // ...
    "react-hooks/exhaustive-deps":  "off"
  }
}

3. If you want to disable it once:
useEffect(() => {
  getList()
}, [data]) // eslint-disable-line

or
/* eslint-disable react-hooks/exhaustive-deps */
useEffect(() => {
  getList()
}, [data])
/* eslint-enable react-hooks/exhaustive-deps */

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question