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

How to keep nested routes states after top-level changed

Open camsong opened this issue 12 years ago • 3 comments

I have a two-level router looks like this, but not the same in real app:

snip20140922_14

  • The top level is in the left, has 3 items, I named them 'myRepo', 'myAssigned', 'meCreated'.
  • The second level is in the top right, can have many children, currently they are 'open' and 'closed' just for demo purpose.

What I want is:

  1. The user selected a state in both top and second level.
  2. When the user changed the top level, the second level should keep it's previous state. like if 'Closed' is selected, it will keep selected whatever the user select in the left.

My routes looks like this. I know the second is ugly, dunno how to make it better :worried:

<Routes location='hash'>
  <Route name='myRepo' handler={MyRepo}>
    <Route name='myRepoOpen'/>
    <Route name='myRepoClosed'/>
  </Route>
  <Route name='myAssigned' handler={MyAssigned}>
    <Route name='myAssignedOpen'/>
    <Route name='myAssignedClosed'/>
  </Route>
  <Route name='meCreated' handler={MeCreated}>
    <Route name='meCreatedOpen'/>
    <Route name='meCreatedClosed'/>
  </Route>
</Routes>

Any suggestion is really appreciated, thanks.

camsong avatar Sep 22 '14 16:09 camsong

Unless you really want those urls, this is a use-case for query params. Otherwise you have to create a cartesian product of ever possible filter and then somehow manage that state and then render all of your links with that state.

<Route name="issues" handler={Issues}/>
var Issues  = React.createClass({
  render: function() {
    return <IssueList user={this.props.query.user} status={this.props.query.status}/>
  }
});
/issues?user=1&status=open

ryanflorence avatar Sep 22 '14 20:09 ryanflorence

I agree about the use of query params.

But as for implementing this with your given route config, I think it would be sufficient to adjust the destination routes of your top level navigation links to the current filter (open vs closed), wouldn't it? If so, you could use an ActiveState mixin in that navigation component and change the destination routes based on the current route (e.g. if meCreatedClosed is active, link to myAssignedClosed and myRepoClosed). Or, if you render those links from the second level handlers, you could simply pass an "isOpen" flag to choose the routes accordingly.

agundermann avatar Sep 22 '14 20:09 agundermann

@taurose

pass an "isOpen" flag to choose the routes accordingly.

'isOpen' belongs to the second level navigation, how to pass it upper to the top level?

@rpflorence Your solution is quite inspiring, you just dispatch all routes to 'Issues'. What if the content of 'Issues' can be quite different based on 2 levels of navigation, I still want to take advantage of react routes(let route define what to show, not 'Issues' component).

camsong avatar Sep 23 '14 02:09 camsong