I’m using customHook to fetch data from an API.
const useFetch = () => {
const dispatch = useDispatch();
return async (callback, ...props) => {
try {
return await callback(...props);
} catch (error) {
const { msg } = error.response?.data || "Something went wrong";
dispatch(showModal(msg));
setTimeout(() => dispatch(hideModal()), 3000);
}
};
};
and using it inside useEffect
const customFetch = useFetch();
useEffect(() => {
(async () => {
const data = await customFetch(fetchUsers, token);
if (data) setUsers(data.user);
})();
}, [token]);
But eslint is complaining about the missing customFetch dependency. If I add it it will end up in an infinite loop. How can I fix this?