admin管理员组文章数量:1193750
I have this useEffect which I use just for cleaning:
useEffect(() => {
return function cleanup() {
if (!room || !currentPortal) return;
leavePortal(
room,
currentPortal,
currentUserProfile && currentUserProfile.uid
? currentUserProfile.uid
: uniqueId
);
detachListener();
};
}, [isFirstLoad, currentUserProfile, currentPortal]);
I can go back and forth and it works just fine, but does nothing if the tab is closed. Is that how useEffect works? Does it not detect the tab closing?
I have this useEffect which I use just for cleaning:
useEffect(() => {
return function cleanup() {
if (!room || !currentPortal) return;
leavePortal(
room,
currentPortal,
currentUserProfile && currentUserProfile.uid
? currentUserProfile.uid
: uniqueId
);
detachListener();
};
}, [isFirstLoad, currentUserProfile, currentPortal]);
I can go back and forth and it works just fine, but does nothing if the tab is closed. Is that how useEffect works? Does it not detect the tab closing?
Share Improve this question asked Apr 19, 2020 at 18:56 TsabaryTsabary 3,9284 gold badges39 silver badges91 bronze badges 5 |1 Answer
Reset to default 30useEffect
will not detect tab close by default.
However you can implement that by yourself:
useEffect(() => {
const cleanup = () => {
// do your cleanup
}
window.addEventListener('beforeunload', cleanup);
return () => {
window.removeEventListener('beforeunload', cleanup);
}
}, []);
本文标签: javascriptuseEffect cleaning function is not called when tab is closedStack Overflow
版权声明:本文标题:javascript - useEffect cleaning function is not called when tab is closed - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738449695a2087413.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
beforeunload
listener for that. – kevmo314 Commented Apr 19, 2020 at 18:57