#Detect leaving a route with react-router-dom 6.21.3

1 messages · Page 1 of 1 (latest)

oak willow
#

🦄 Hi everyone,

I’m building a really complex app to do test for paramedics in my organization (it’s like the Red Cross).

I wanna warn the user if they’re about to change the route (like “deactivation” of an Angular route).

I saw that react-router-dom used to have “useBlocker” and “useCallbackPrompt” but those no longer exist (and also NavigationContext)
Any ideas on how I can solve this problem? ✨

pure sparrow
#

I’m pretty sure useBlocker was removed but brought back, maybe you didn’t know it came back?

#

But if you did want to make a custom useBlocker it should just be as easy as a Blocker component or mimicking the useBlocker API if you like it. Then under the hood you just have a Boolean useState of “warnedUser” or something and on first click change warnedUser next click do the actual action

oak willow
#

Hi there! Thank you for your help

#

useBlocker does exist 🙂

#

I just used it thanks to you

#

This isn’t enough though to get when I need.. what if someone changes the route using Chrome?

oak willow
#

Any idea on how I can solve this?

pure sparrow
#

I don’t believe web browsers want you to be able to hijack the users ability to navigate away to another page. So I think them typing in directly to the url bar or hitting the back button doesn’t have any way to be hooked into via JavaScript. If it did though, you’d probably be looking for something on the browser window object

oak willow
#

It’s just that my paramedics are doing an important test.. if they leave the page I just wanna warn them.

pure sparrow