0

I want to redirect to previous page when the current page is reloaded or refreshed,

constructor(props) {
    super(props);
    this.onUnload = this.onUnload.bind(this);
    this.onbeforeunload= this.onbeforeunload.bind(this)
    this.state = {
      show_answers: [],
      other_data: [],
      question: "",
      answer: "",
      id: 0,
    };
  }



onUnload() {
    console.log("reloads")
this.context.router.push('/getanswer');
}

onbeforeunload(){
  console.log("reloadsssss")
this.context.router.push('/getanswer');
}

I have tried above way but not working any suggestions.

Nabeel Ayub
  • 1,060
  • 3
  • 15
  • 35

2 Answers2

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

whenever page loads componentDidUpdate method will get invoke and it will redirect into previous page.

Sajish
  • 1
  • 1
0

ReactJs don't have any method to listen reload events. Use this plain Javascript event handler:

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

Usa can also put this (on constructor):

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