【发布时间】:2022-01-11 01:03:55
【问题描述】:
我正在使用 customHook 从 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);
}
};
};
并在 useEffect 中使用它
const customFetch = useFetch();
useEffect(() => {
(async () => {
const data = await customFetch(fetchUsers, token);
if (data) setUsers(data.user);
})();
}, [token]);
但是 eslint 抱怨缺少 customFetch 依赖项。如果我添加它,它将以无限循环结束。我该如何解决这个问题?
【问题讨论】:
标签: javascript reactjs use-effect