美文网首页
react中如何判断页面刷新

react中如何判断页面刷新

作者: 粥蛋蛋 | 来源:发表于2021-06-24 17:13 被阅读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');
          }
     }
    

    亲测有效

    相关文章

      网友评论

          本文标题:react中如何判断页面刷新

          本文链接:https://www.haomeiwen.com/subject/fkhgyltx.html