我无法确定在这个useEffect钩子中应用什么清理函数。如果没有清理函数,它可以正常工作。
useEffect(() => {
const fetchProfileUser = async () => {
if (profileUserId === existingUser._id) {
setprofileUser(existingUser);
} else {
const profileUser = await UserApi.getUser(profileUserId);
setprofileUser(profileUser);
}
};
fetchProfileUser();
}); Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
你不总是需要一个清理函数。但大多数情况下,在
useEffect中你需要一个依赖数组,即使它是空的。如果你在进行请求,你肯定需要一个依赖数组。如果你在进行请求,你可以使用AbortController来取消请求,使用清理函数,如果该组件被卸载。