react-router icon indicating copy to clipboard operation
react-router copied to clipboard

Feature: Router.transitionToParentRoute()

Open abergs opened this issue 12 years ago • 13 comments

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?

abergs avatar Aug 12 '14 14:08 abergs

You have Router.goBack(), reopen if that doesn't work :)

ryanflorence avatar Aug 12 '14 20:08 ryanflorence

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

abergs avatar Aug 12 '14 21:08 abergs

Then I think defaultLeaveRoute is a fine solution, either on your route or your route handler.

ryanflorence avatar Aug 12 '14 21:08 ryanflorence

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?

abergs avatar Aug 13 '14 09:08 abergs

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.

abergs avatar Aug 13 '14 11:08 abergs

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?

ryanflorence avatar Aug 13 '14 13:08 ryanflorence

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...

abergs avatar Aug 13 '14 14:08 abergs

To make sure I understand, you want a way to Router.transitionToParentRoute(), which would transition any active child to it's parent?

ryanflorence avatar Aug 13 '14 14:08 ryanflorence

Yes! :+1:

abergs avatar Aug 13 '14 14:08 abergs

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).

ryanflorence avatar Aug 13 '14 14:08 ryanflorence

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? ;)

abergs avatar Aug 13 '14 15:08 abergs

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} />;
}

agundermann avatar Aug 13 '14 15:08 agundermann

^ 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.

ryanflorence avatar Aug 13 '14 15:08 ryanflorence