41

I have a react component with a useEffect hook that looks like this:

const LocationEditor: React.FC<SectionEditorProps> = (props) => {
const [section, setSection] = useState({...(props.section as Location)});
useEffect(() => {
    props.onChange(section);
}, [section]);

I'm getting this warning when I run the project:

React Hook useEffect has a missing dependency: 'props'. Either include it or remove the dependency array. However, 'props' will change when *any* prop changes, so the preferred fix is to destructure the 'props' object outside of the useEffect call and refer to those specific props inside useEffect  react-hooks/exhaustive-deps

So I tried changing the component to look like this, destructuring props:

const LocationEditor: React.FC<SectionEditorProps> = ({section, onClickCancel, onClickSave, onChange}) => {
    const [tmpSection, setSection] = useState({...(section as Location)});
    useEffect(() => {
        onChange(tmpSection);      
    }, [tmpSection]);

If I add this comment before the dependency array, I can make the warning go away:

// eslint-disable-next-line react-hooks/exhaustive-deps

However, when I add the block below to eslintConfig in package.json it doesn't seem to do anything:

{
  "plugins": [
    "react-hooks"
  ],
  "rules": {
    "react-hooks/rules-of-hooks": "error",
    "react-hooks/exhaustive-deps": "off"
  }
}

I see some people talking about using the .eslinrc file, but I thought you could also configure rules globally in package.json and I can't find that file in my project.

Is there something I'm missing here?

5
  • 5
    That is almost certainly a bad idea. Just put the onChange function in your dependency array. Why is everybody's response to the tool, that React team at Facebook itself uses, telling them that they're possibly doing something wrong to immediately want to toss out the tool? I realize it's not just you, we get this question in the react tag all the time, it just never fails to surprise me... Commented Jan 13, 2021 at 15:12
  • Thanks! That was helpful. Basically I fixed it like this: stackoverflow.com/questions/58866796/… Commented Jan 13, 2021 at 16:28
  • 4
    Acknowledging that it's a bad idea to disable this rule, it would still be nice to have an answer to this question for those of us who are stubborn. The linked answer does not address the problem of the package.json config seemingly being ignored. Commented Apr 6, 2021 at 18:52
  • This answer helped me a lot (ignoring warning): stackoverflow.com/questions/63974832/… Commented Jul 2, 2022 at 4:11
  • beta.reactjs.org/learn/… Commented Jan 25, 2023 at 16:02

1 Answer 1

32

Disabling it globally is not recommended as it helps to point out potential bugs. Before disabling the rule locally, check carefully whether it doesn't point to a possible bug.

You can disable it locally with:

// eslint-disable-next-line react-hooks/exhaustive-deps
Sign up to request clarification or add additional context in comments.

4 Comments

thanks! Fixed it per the comment above. Appreciate your help.
"Just do what the rule says" - it's not always the best option. An example: if you need to fetch data from a server, you usually pass to useEffect an empty array as a second argument (because you need to run the callback only once when your component did mount) - and it's an official advice from React documentation. And then "exhaustive-deps" often starts to annoy you with warnings that you "forgot" dependencies (but you're sure you didn't). And adding dependencies you don't need is an antipattern, because it may lead to unnecessary rerenderings or even to an infinite cycle of rerenderings...
This is just partly true, I think. Actually, the official documentation for useState says that it is safe to omit the setter from the dependency array. reactjs.org/docs/hooks-reference.html#usestate Perhaps it is too much to expect from the linter to identify a setter or useReducer-dispatch function that is not included in the dependency array?
Hi, I've tried using the same step, but in my case it's react-hooks/rules-of-hooks, but it keeps warning me, is it React's fault or my ESLint setup?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.