How could I listen beforePopState for prevent de backward navigation using new app router? #49532
Replies: 9 comments 1 reply
-
Did you figure this out? |
Beta Was this translation helpful? Give feedback.
-
Hello @lucasoz ,
|
Beta Was this translation helpful? Give feedback.
-
This is a good start which may be fine for basic implementations, however, it fails to handle more sophisticated requirements, e.g. detecting if a I just had the joy of spending a couple of hours solving this problem thanks to Anyway, here's an outline of my solution for this particular scenario outlined above:
The resultant code looks something like this:
|
Beta Was this translation helpful? Give feedback.
-
Any update on this @leerob? |
Beta Was this translation helpful? Give feedback.
-
Any update on this? |
Beta Was this translation helpful? Give feedback.
-
Any update on this? |
Beta Was this translation helpful? Give feedback.
-
Here only to add some information I found while researching a way to achieve this in Next. React Router has a hook To my understanding, implementing something like this in Next would require tapping into the router internals. A big effort for what is, IMO, a small UX improvement (if any). Their implementation of useBlocker accesses their route state context. Since my use case is the incomplete form, I will follow their suggestion and persist the state in session storage, rather than blocking that page navigation. IMO, better UX and small tech effort. Hope this helps :) |
Beta Was this translation helpful? Give feedback.
-
Updates? |
Beta Was this translation helpful? Give feedback.
-
We need this to provide a better UX... |
Beta Was this translation helpful? Give feedback.
-
I'm trying to catch the beforePopState event and use it to close a modal in mobile devices but the property doesn't exist in new app router.
Property 'beforePopState' does not exist on type 'AppRouterInstance'.
Is there another pattern to achieve this behavior?
Beta Was this translation helpful? Give feedback.
All reactions