admin管理员组文章数量:1391947
Warning: Can't perform a React state update on an unmounted ponent. This is a no-op, but it indicates a memory leak in your application. To fix, cancel all subscriptions and asynchronous tasks in a useEffect cleanup function.
useEffect(() => {
const unsubscribe = streamCourses({
next: (querySnapshot) => {
const task = querySnapshot.docs.map((docSnapshot) =>
mapDocTask(docSnapshot)
);
setCourseDetails(task);
},
error: (error) => console.log(error),
});
return unsubscribe;
}, [setCourseDetails]);
Warning: Can't perform a React state update on an unmounted ponent. This is a no-op, but it indicates a memory leak in your application. To fix, cancel all subscriptions and asynchronous tasks in a useEffect cleanup function.
useEffect(() => {
const unsubscribe = streamCourses({
next: (querySnapshot) => {
const task = querySnapshot.docs.map((docSnapshot) =>
mapDocTask(docSnapshot)
);
setCourseDetails(task);
},
error: (error) => console.log(error),
});
return unsubscribe;
}, [setCourseDetails]);
Share
Improve this question
asked Oct 27, 2021 at 6:27
P A T HimarangaP A T Himaranga
1111 gold badge2 silver badges5 bronze badges
2
- HI please include some details of the scenario you are having with. It will help us understand your question – German Commented Oct 27, 2021 at 7:11
-
Why is there a
setCourseDetails
there? And what does it do? – Mario Nikolaus Commented Oct 27, 2021 at 7:36
1 Answer
Reset to default 5I had a similar issue to this. What I had to do to solve it was two things:
(1) I created a State
boolean isMounted
which was set to true
by default and was used to wrap the contents of my useEffect
s so that the contents of my useEffect
s would only run if the screen was mounted
.
(2) I created a useEffect
dedicated solely to cleanup. Meaning this useEffect
had nothing besides a return
statement in it which set the various State
variables I had to their default values.
Example:
useEffect(() => {
if (isMounted) {
const unsubscribe = streamCourses({
next: (querySnapshot) => {
const task = querySnapshot.docs.map((docSnapshot) =>
mapDocTask(docSnapshot)
);
setCourseDetails(task);
},
error: (error) => console.log(error),
});
return unsubscribe;
}
}, [setCourseDetails]);
useEffect(() => {
return () => {
setCourseDetails(null);
setIsMounted(false);
}
}, []);
本文标签:
版权声明:本文标题:javascript - How to fix, cancel all subscriptions and asynchronous tasks in a useEffect cleanup function error - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744709863a2621064.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论