我在用着React js。我需要检测page refresh
。当用户点击刷新图标或按 F5 时,我需要找出事件。
我尝试过堆栈溢出帖子通过使用 JavaScript 函数
我使用了javascript函数beforeunload
还是没有运气。
onUnload(event) {
alert('page Refreshed')
}
componentDidMount() {
window.addEventListener("beforeunload", this.onUnload)
}
componentWillUnmount() {
window.removeEventListener("beforeunload", this.onUnload)
}
这里我有完整的代码堆栈闪电战
如果您使用 React Hook、UseEffect,您可以在组件中进行以下更改。这对我有用
useEffect(() => {
window.addEventListener("beforeunload", alertUser);
return () => {
window.removeEventListener("beforeunload", alertUser);
};
}, []);
const alertUser = (e) => {
e.preventDefault();
e.returnValue = "";
};
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)