Well if you want to look into this differently, you just need to know what are options does the React has that non exhaustive-deps
? One of the reason you should not use a closure function inside the effect is on every render, it will be re-created/destroy again.
So there are multiple React methods in hooks that is considered stable and non-exhausted where you do not have to apply to the useEffect
dependencies, and in turn will not break the rules engagement of react-hooks/exhaustive-deps
. For example the second return variable of useReducer
or useState
which is a function.
const [,dispatch] = useReducer(reducer, {});
useEffect(() => {
dispatch(); // non-exhausted, eslint won't nag about this
}, []);
So in turn you can have all your external dependencies together with your current dependencies coexist together within your reducer function.
const [,dispatch] = useReducer((current, update) => {
const { foobar } = update;
// logic
return { ...current, ...update };
}), {});
const [foobar, setFoobar] = useState(false);
useEffect(() => {
dispatch({ foobar }); // non-exhausted `dispatch` function
}, [foobar]);