onUnload(event) {
alert('頁面刷新')
}
componentDidMount() {
window.addEventListener("beforeunload", this.onUnload)
}
componentWillUnmount() {
window.removeEventListener("beforeunload", this.onUnload)
}
beforeunload方法在react文件里根本不管用,后來搜到的解決方案如下:
if (window.performance) {
if (performance.navigation.type == 1) {
//表示頁面刷新了
sessionStorage.removeItem('couponNumber');
}
}
親測有效