1

Suppose a user comes to some page of my website. Here user have clicked on some shared link which opens a new tab in users' browser.

Now if user clicks on back button, then user should land on home page instead of going back to blank page of browser.

I tried using history.listen method provided by react-router. But I am not able to redirect user to home page.

here is the code snippet

componentDidMount(){
  this.props.history.listen((location, action) => {
    if (action === 'POP') {
       this.props.history.push('/homepage');
    }
}
user10169731
  • 131
  • 11
  • I have given the answer in here. This might help: [https://stackoverflow.com/questions/55966533/show-alert-on-browser-back-button-event-on-react-js](https://stackoverflow.com/questions/55966533/show-alert-on-browser-back-button-event-on-react-js) – Ashok R Raghu Nov 26 '19 at 09:39

1 Answers1

1

from what page user should click back button to go back from the new tab or the same tab. and if you open a new tab user can't go back because there's no page visited earlier on that tab and if you want to take the user user after the shared button has been click you can set a click event on shared button and take the user back to home page.

takeUserToHomepage=()=>{
  this.props.history.push('/homepage')
}

<a target="_blank" onClick={this.takeUserToHomepage} href="https://www.naveenkashyap.com/">My Site</a>
Naveen Kashyap
  • 372
  • 1
  • 9
  • or you can use window.history on your componentDidMount to add url in you history https://developer.mozilla.org/en-US/docs/Web/API/hello-world.html and when click back it will take you to your desired url – Naveen Kashyap Nov 26 '19 at 09:55