I want to dispatch
some action when my React component unmounts. As I am using hooks, I can't use componentWillUnmount
. Instead, I am using the useEffect
hook:
const dispatch = useDispatch();
useEffect(() => () => dispatch(setValue()), []);
Above code throws the warning "React Hook useEffect has a missing dependency: 'dispatch'. Either include it or remove the dependency array". I can't include dispatch
in dependencies array as it will cause useEffect
to run whenever dispatch
changes, but I want it to run only when the component is unmounting; and for that I need to pass an empty array.
How can I fix this warning without removing the corresponding linting rule from babel?