刷新当前页面时如何重定向到另一个页面

时间:2020-03-03 08:22:59

标签: reactjs react-router react-router-dom

我要在重新加载或刷新当前页面时重定向到上一页,

function openNav() {
  document.getElementById("myNav").classList.add("active");
}

/* Close when someone clicks on the "x" symbol inside the overlay */
function closeNav() {
  document.getElementById("myNav").classList.remove("active");
}

我已经尝试了上述方法,但是没有任何建议。

2 个答案:

答案 0 :(得分:0)

componentDidUpdate() {
  this.context.router.push('/getanswer');
}    

页面加载时,componentDidUpdate方法将被调用,并将重定向到上一页。

答案 1 :(得分:0)

ReactJs没有任何方法来监听重载事件。使用以下简单的Javascript事件处理程序:

window.onbeforeunload = (e) => {
  this.context.router.push('/getanswer');
};

美国航空也可以将其放在构造函数上:

constructor(props) {
  if (window.performance) {
    if (window.performance.navigation.type == 1) {
      //This page is reloaded
      this.context.router.push('/getanswer');
    }
  }
}