How could I listen beforePopState for prevent de backward navigation using new app router? #49532
Replies: 12 comments 3 replies
-
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.
-
Any Updates? |
Beta Was this translation helpful? Give feedback.
-
Hey everyone, I wanted to share one solution that might be relevant for you all. I saw a few comments in here related to handing popstate and the browser back button, so figured I'd write this up. Here was my use case:
I can definitely empathize with this being confusing, and apologize we haven't done enough to create examples or show how this could be possible. For example, trying to animate across routes using The solution I landed on is this:
This isn't perfect – for example, if you manually swipe back in iOS Safari. The most ideal solution here is View Transitions, which you can use https://next-view-transitions.vercel.app for in some cases. However, I couldn't get it working perfectly for the animation I was going for in this case, and also there isn't Firefox support yet for View Transitions. I do believe this is the best solution in the future though. CleanShot.2024-12-29.at.14.56.21.mp4Hope this helps! 🙏 (x-post) |
Beta Was this translation helpful? Give feedback.
-
My use case was really similar to the author of this discussion, I wanted my modal to close when the user presses the back button of its browser, instead of navigating to the previous page (was for better UX on mobile mainly). Here is how I did it:
|
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
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