Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent navigation with React router v6

I want to popup a modal if a use leaves a form page that tells them their changes will be lost if they leave and they have the option to leave or cancel.

I saw that recent changes to V6 from beta takes out the use of Prompt, useBlocker, and useHistory. I have seen answers for using onBeforeUnload but that seems to only use a built in prompt.

Is there a way to do this with the useNavigate hook?

like image 360
Kasie Chaplick Avatar asked Feb 27 '26 10:02

Kasie Chaplick


1 Answers

Here's a similar issue on GitHub https://github.com/remix-run/react-router/issues/8139.

A possible solution is using UNSAFE_NavigationContext if it exposes block option. See https://github.com/remix-run/react-router/issues/8139#issuecomment-977790637.

I tested it with "react-router-dom": "^6.0.2" and it works [as a workaround] for now.

like image 174
Agon Qorolli Avatar answered Mar 02 '26 14:03

Agon Qorolli



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!