将状态设置在Axios函数调用的内部,然后在useffect Hook中调用函数
我正在使用反应功能组件。 我将状态设置在Axios调用的内部,然后在USEFFECT挂钩中调用该函数。
这是我的功能:
import { useDispatch } from “react-redux”;
functon ABCComponent({ navigation }){
const dispatch = useDispatch();
const testPostID = "1234567891011";
const [post, setPost] = useState("");
const getPostById = async(postID)=>{
await axios.get(`http://localhost:5000/posts/post/${postID}`)
.then((response) =>{
dispatch(setLoading(true));
dispatch(setSuccess(true));
dispatch(setLoading(false));
response.data.message.map((post, index) =>{
setPost(post); // I think it's complaining about this line
});
}).catch((error) =>{
console.log("err: ", error);
dispatch(setLoading(true));
dispatch(setSuccess(false));
dispatch(setLoading(false));
dispatch(setMessage(`${error}`));
});
};
useEffect(() =>{
getPostById(testPostID);
}, []);
}
我收到以下错误:
警告:无法在未填充组件上执行React状态更新。这是一个无操作,但表示您的应用程序中的内存泄漏。要修复,请在%s。%s中取消所有订阅和异步任务,使用效率清理功能, 在abccomponent。
我尝试的是: 我尝试删除异步/等待,希望它能解决问题,但事实并非如此。
解决此问题的最佳方法是什么?
I am using React functional components.
I am setting state inside of an axios call, then calling the function in useEffect hook.
Here is my function:
import { useDispatch } from “react-redux”;
functon ABCComponent({ navigation }){
const dispatch = useDispatch();
const testPostID = "1234567891011";
const [post, setPost] = useState("");
const getPostById = async(postID)=>{
await axios.get(`http://localhost:5000/posts/post/${postID}`)
.then((response) =>{
dispatch(setLoading(true));
dispatch(setSuccess(true));
dispatch(setLoading(false));
response.data.message.map((post, index) =>{
setPost(post); // I think it's complaining about this line
});
}).catch((error) =>{
console.log("err: ", error);
dispatch(setLoading(true));
dispatch(setSuccess(false));
dispatch(setLoading(false));
dispatch(setMessage(`${error}`));
});
};
useEffect(() =>{
getPostById(testPostID);
}, []);
}
I am getting the following error:
Warning: Can't perform a React state update on an unmounted component. This is a no-op, but it indicates a memory leak in your application. To fix, cancel all subscriptions and asynchronous tasks in %s.%s, a useEffect cleanup function,
in ABCComponent.
What I tried:
I tried removing async/await hoping that it would solve the problem, but it didn’t.
what is the best way to go about solving this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如警告所述,当您尝试更新未填充组件上的状态时,可能会发生内存泄漏。为了解决此问题,您应该包括一些
iSMounted
标志,以便在更新状态之前检查组件是否仍在安装。另外,还有其他一些建议我会给您 - 不要使用异步/等待。然后,您应该仅使用这两种方法中的一种(我的建议是async/等待)。您应该重写类似的内容:
还有一个不清楚的部分,您要更新状态,为什么使用.map以及为什么在每次迭代中覆盖状态?
Memory leak can occur when you are trying to update state on unmounted component, like the warning said. In order to fix this you should include some
isMounted
flag that you will use in order to check whether component is still mounted, before updating state. Also there are few other suggests I would give you - do not use async/await with .then, you should use only one of these two approach(async/await is my suggestion).You should rewrite to something like this:
Also there is one unclear part where you are updating state, why using .map and why overriding state in each iteration?