Search code examples
javascriptreactjsreact-reduxwarningsdispatch

useEffect has a missing dependency: 'dispatch'


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 !


Solution

  • 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]);