react中如何判断页面刷新

2021-06-24  本文已影响0人  粥蛋蛋
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');
      }
 }

亲测有效

上一篇 下一篇

猜你喜欢

热点阅读