简体   繁体   中英

How to redirect to home page after clicking browser back button, when on a particular page

I am creating a flight booking website using the MERN stack. The final confirmation page shows a receipt and flight ticket. I want to redirect the user to the home page on clicking the back button, while on the confirmation page. I am using react-router-dom.

Short answer: You can't change the behavior of the Browser Back Button.

Long answer:

Browser Back button looks for a history stack maintained by the browser itself, Its not possible to override the action on that button.

Workaround:

What of we tell each of your route components(components/screen/page attached with to the path) to check if the last location visited in the application is /confirmation-page then redirect to /home .

But it won't be a good practice!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM