0

I am using Reactjs and Redux.

//constants
const dispatch = useDispatch();

//useEffects
useEffect(() => {
  if (!IsEmpty(loggedUser)) {
    dispatch(actions.getUserDetail({ userId: loggedUser.Sid }));
  }
}, [loggedUser]);

Everything is working fine. But I am getting a warning in browser's console :

src/components/EditProfile.js Line 91:6: React Hook useEffect has a missing dependency: 'dispatch'. Either include it or remove the dependency array react-hooks/exhaustive-deps

I don't know how can I remove this warning.
Please help me !

Abhishek kamal
  • 319
  • 3
  • 12
  • Does this answer your question? [React Hook useEffect has a missing dependency: 'dispatch'](https://stackoverflow.com/questions/58624200/react-hook-useeffect-has-a-missing-dependency-dispatch) – John Lobo Mar 25 '22 at 05:29
  • Some are saying, you can add`dispatch` as dependency array in `useEffect`. Is it safe ? – Abhishek kamal Mar 25 '22 at 05:37
  • @JohnLobo The link you provided, is not my answer. Because If I use `useCallback` and move dispatch inside that useCallback then I am getting the same warning. – Abhishek kamal Mar 25 '22 at 05:46

1 Answers1

2

If you want to remove warnings like React Hook useEffect has a missing dependency: 'dispatch' then you can use eslint-disable-next-line in your useEffect.

useEffect(() => {
  if (!IsEmpty(loggedUser)) {
    dispatch(actions.getUserDetail({ userId: loggedUser.Sid }));
  }
  //eslint-disable-next-line
}, [loggedUser]);
Dharmik Patel
  • 1,041
  • 6
  • 12