【问题标题】:React js detect page reloadReact js检测页面重新加载
【发布时间】:2021-02-15 10:55:02
【问题描述】:

使用 React.js,我如何检查整个页面是否重新加载?

if (window.performance) {
      console.info("window.performance works fine on this browser");
    }
    console.info(performance.navigation.type);
    if (performance.navigation.type == performance.navigation.TYPE_RELOAD) {
      console.info("This page is reloaded");
    } else {
      console.info("This page is not reloaded");
    }

上面的代码没有帮助,每次我更改组件或调用同一页面上的任何 api 时都会触发。

【问题讨论】:

  • 您可以在每次页面访问时将当前 URL 放入 pastURL sessionStorage 项中,然后在此之前检查pastURL 是否与当前 URL 匹配?
  • @evolutionxbox 问题是我的 pastUrl 与 currentUrl 相同,例如:www.test.com

标签: javascript reactjs typescript reload


【解决方案1】:

试试这个

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

componentDidMount() {
    window.onbeforeunload = function() {
        this.onUnload();
        return "";
    }.bind(this);
}

【讨论】:

    猜你喜欢
    • 2018-05-02
    • 1970-01-01
    • 2018-05-02
    • 1970-01-01
    • 2019-05-17
    • 2016-06-08
    • 2022-11-10
    • 2018-02-07
    • 1970-01-01
    相关资源
    最近更新 更多