Where is the end of the humain?

React 快速检测用户是否离开

在主 class 中直接调用函数,函数实现细节如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
const useLeave = (event: Event) => {
const customEvent = event as CustomEvent;
if (customEvent.detail.url != ' path that you need to put here '){
// do the action that you want
}

useEffect(()=>{
document.addEventListener('oxg-header--navigate',leave);
return ()=>document.removeEventListenser('oxg-header--navigate',leave);
},[]);

return null;
}

export default useLeave;

© 2026 iattachの小站

Elegant theme by Shiro · Made by Acris with ❤️