Feature: Router.transitionToParentRoute()
I have a route which triggers a modal and I want the modal to be linkable, so it has it's own route. However, when closing the modal, I want to go back to the parent route. Currently I hardcode the name of the parent route as an extra prop like this:
<Route name="booking/quickEdit" path="/booking/quickEdit/:bookingId" handler={EditBookingModal} defaultLeaveRoute="booking/overview" />
...
<Route name="booking/quickEdit" path="/booking/quickEdit/:bookingId" handler={EditBookingModal} defaultLeaveRoute="booking/customerInfo" />
What do you think, good idea?
You have Router.goBack(), reopen if that doesn't work :)
I'm unable to reopen the Issue, but that wouldn't solve the problem. For example if the child route is the entry point than goBack would throw you out of the application
Then I think defaultLeaveRoute is a fine solution, either on your route or your route handler.
Let's say my parent route has some dynamic segments, like this:
/booking/:day/
/booking/:day/quickEdit/:bookingId
than my defaultLeaveRoute wouldn't be able to handle it without manually specifying the dynamic segments in some other prop. The transitionTo('..') could resolve it by reading the current URL and match the arguments automatically to the parent route, which would be a lot nicer!
Wouldn't you agree?
Temporary workaround solved this by passing this.props.params from the component triggering the transitionTo, but I don't like that solution, since we are passing more params than expected and needed.
Its fine, the unknown params won't make it into the target handler.
Do you have a sample API of what you'd prefer to do?
The consumer would simply issue Router.transitionTo('..') or perhaps Router.goUp() (similar to Router.goBack()) in a nested route.
For how to handle it internally i guess we could store a reference to the parent route, since we have the relationship when registering it in the RouteStore:
registerRoute: function (route, _parentRoute) {
...
route.parent = _parentRoute;
...
}
and in transitionTo we could detect name == .., get the currentRoute.parent.nameand transitionTo it.
To allow the case for ../../ we could just probably do something as simple as
n = split("..").length
// and reach into the parent n times
var finalRoute;
while(n-- > 0)
finalRoute = route.parent
transitionTo(finalRoute.name)
What do you think? I'm not that familiar with core just yet, but this is top of my head...
To make sure I understand, you want a way to Router.transitionToParentRoute(), which would transition any active child to it's parent?
Yes! :+1:
Seems weird ...I'd need a lot more convincing that knowing the name of my parent route is difficult, especially when we already have a nice way to handle this (as discussed already).
Well it would abstract away the need of adding a custom prop to every route that needs to access it's parent. And in our case (where we have a generic dialog component, it makes a lot of sense).
In my world, it would be natural API for a Router, but I'm not the genius-router-guru here, so if you think it's dumb, it probably is. Perhaps one of the other contributers can shoot it down as well? ;)
Not sure I understand the example completely, but have you thought about passing callbacks down the hierarchy? For example, in your /booking/:day handler:
handleNavigateUp: function(){
Router.transitionTo(..);
},
render: function(){
return <this.props.activeRouteHandler onNavigateUp={this.handleNavigateUp} />;
}
^ this is a great solution, I was actually typing it out and then yours popped up :P
You don't know anything about your parents in React, and I think I'd like to keep the same paradigm in the router.