gpt4 book ai didi

javascript - react |如何检测页面刷新(F5)

转载 作者:数据小太阳 更新时间:2023-10-29 05:46:10 26 4
gpt4 key购买 nike

我正在使用 React js .我需要检测页面刷新。当用户点击刷新图标或按 F5 时,我需要找出事件。

我试过 stackoverflow post通过使用 javascript 函数

我使用了 javascript 函数 beforeunload 仍然没有成功。

onUnload(event) { 
alert('page Refreshed')
}

componentDidMount() {
window.addEventListener("beforeunload", this.onUnload)
}

componentWillUnmount() {
window.removeEventListener("beforeunload", this.onUnload)
}

这里我有关于 stackblitz 的完整代码

最佳答案

将它放在构造函数中:

if (window.performance) {
if (performance.navigation.type == 1) {
alert( "This page is reloaded" );
} else {
alert( "This page is not reloaded");
}
}

它会起作用,请在 stackblitz 上查看此示例.

关于javascript - react |如何检测页面刷新(F5),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50026028/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com