-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Some kind of onbeforeunload
for routes?
#22
Comments
Yeah, there currently isn't a way to do this. I agree it would be a worthwhile feature to add. Thinking about it brings up a lot of questions how exactly it should work and what the expectations are. Just thinking out loud here: The handlers would have to operate at the router/location level but you would likely want to register them on a route close to your form or whatever (like you illustrated). So if you had multiple registered on say a parent route and a child route, and the location change affected both routes, would they both fire? Or maybe just the root-most one? If they did both fire then if either rejected the transition would be cancelled. Cancellation of navigation initiated from the router would be easy enough to handle but locations changes from the integrated system like I'm going to think about this a bit and see how others like React-Router handle this. |
It's definitely tricky. Just my two cents:
Interestingly, React Router doesn't seem to support routing events, per se. They expose a <Prompt when={isBlocking}
message={location =>`Are you sure you want to go to ${location.pathname}`}
/> Haven't dug into the internals there, and I'm not sure how I feel about that... Actually, I am sure. "Prompt" is a horribly named component. 😄 But using a component to control the state of whether a navigation should be allowed is intriguing. Perhaps this is cleaner, come to think of it. You don't really have hooks to intercept route changes, but you do have a way to prevent navigation without notifying the user. Granted, component or no component, the internals would need to support all the prevention and rolling back of the navigation state. Alright, now I'm just rambling. Sorry. For the app I'm working on, I'm moving to an "all CRUD objects are mutated in modal dialogs" style of UI. It doesn't really address the back button issue, but I can easily disable/disallow internal navigation this way. |
Perhaps I'm missing something, but is there a way to register a callback for route changes... And cancel them? Like an
onbeforeunload
but for routes?I don't know about the api specifically, that's just as an example of potential usage.
The text was updated successfully, but these errors were encountered: