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

how to <Link> to a nested route?

Open tjwebb opened this issue 11 years ago • 7 comments

<Route name='a'>
  <Route name='b' />
</Route>

I want to <Link to='a/b'> but that doesn't work. to='b' doesn't work either. I get Unable to find <Route name="b">. What am I missing?

tjwebb avatar Feb 05 '15 02:02 tjwebb

I realized I can't use a nested route under a DefaultRoute. So then how do I have a default route?

tjwebb avatar Feb 05 '15 02:02 tjwebb

Are you trying to redirect? DefaultRoute matches its parent's path, so it becomes the RouteHandler in the parent's render.

On Wednesday, February 4, 2015, Travis Webb [email protected] wrote:

I realized I can't use a nested route under a DefaultRoute. So then how do I have a default route?

— Reply to this email directly or view it on GitHub https://github.com/rackt/react-router/issues/772#issuecomment-72985859.

ryanflorence avatar Feb 05 '15 03:02 ryanflorence

Let's say we have the following configuration :

<Route name="app" handler={App}>
  <Route name="user" handler={User}>
    <Route name="profile" handler={Profile} />
  </Route>
<Route>

How can we provide a default route from /to /user/profile?

salper avatar Feb 10 '15 15:02 salper

@salper Does <Redirect from='/' to='/user/profile' /> help? I'm not sure if it's idiomatic way of doing that though.

gaearon avatar Feb 10 '15 16:02 gaearon

@gaearon Well, that was the main (hidden) question. I wanted to know if there was a way to do so using DefaultRoute component. I'll go with the Redirect one til something idiomatic comes up. Thx.

salper avatar Feb 10 '15 16:02 salper

To me it sounds like redirection is exactly what you're looking for.

How can we provide a default route from /to /user/profile?

The way I see it, DefaultRoute is not meant to affect your URLs in any way, or allow you to show the same content for different urls, but to give your parent routes a default RouteHandler in case they match the url exactly (and no child route does). A bit like index.html on webservers.

If you really wanted to use the same handlers for / and /user/profile, I think you could simply use routes with path="/":

<Route name="app" path="/" handler={App}>
  <Route name="user-index" path="/" handler={User}>
    <Route name="profile-index" path="/" handler={Profile} />
  </Route>
  <Route name="user" handler={User}>
    <Route name="profile" handler={Profile} />
  </Route>
<Route>

agundermann avatar Feb 10 '15 18:02 agundermann

That sounds pretty well to me, thx.

salper avatar Feb 11 '15 08:02 salper