1

I am building a website in react with multiple pages using react-router-dom and I want to be able to handle the equivalent of a window 'beforeunload' event whenever I am about to switch pages using react-router. I am pretty new to react, so maybe there is an obvious solution I'm not seeing?

I have tried useEffect with history.block as shown here, and just adding a window 'beforeunload' event, but this only seems to work for reloading, closing, or leaving the domain, not for clicking internal links or back/forward navigation.

  • The component unmounting when the route changes would be the "event" you want to handle. `beforeunload` is the case when the user does any of those other actions you listed. Can you [edit] the post to include a [mcve] of the relevant code you are working with, and provide more details about what exactly isn't working as expected? – Drew Reese Feb 23 '23 at 00:54

0 Answers0