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

DefaultRoute doesn't work with children

Open 0x80 opened this issue 11 years ago • 7 comments

I was trying to do this

  <Route name="root" path="/" handler={Master}>
      <Route name="composer" handler={Composer}>
        <DefaultRoute name="browse" path="browse" handler={Browse}>
          <Route name="browse-details" path=":id" handler={BrowseDetails}/>
        </DefaultRoute>
        <Route name="create" handler={Create} />
        <Route name="arrange" handler={Arrange} />
      </Route>
      <Route name="conductor" handler={Conductor} />
      <Route name="performer" handler={Performer} />
      <DefaultRoute name="home" handler={Home}/>
  </Route>

... but a DefaultRoute doesn't seem to work with children. How can I make it so that if you browse to "/composer" you end up at /composer/browse?

I've tried adding <Redirect from="composer" to="browse"/> at the bottom as the last child of root, but that didn't seem to do anything.

Maybe I'm misunderstanding something in the API?

0x80 avatar Jan 12 '15 16:01 0x80

Ran into this too

kentor avatar Jan 14 '15 21:01 kentor

I encountered the same thing. What worked for me was using an absolute path for the redirect component. So /dashboard will be redirected to /dashboard/company.

Related issue: https://github.com/rackt/react-router/issues/622

    <Route name='dashboard' path='dashboard' handler={Dashboard}>
      <Redirect from="/dashboard" to="company"/>

      <Route name='company' path='company' handler={CompanyProfile} />
      <Route name='activities' path='activities' handler={Activities} />
    </Route>

aybarshazar avatar Jan 23 '15 13:01 aybarshazar

Still would be nice not to change the url.

kentor avatar Jan 23 '15 15:01 kentor

@0x80 <DefaultRoute> isn't supposed to have children. It's just a way to declare a route that will be rendered when none of its siblings match. See also https://github.com/rackt/react-router/issues/701

Try this instead:

<Route name="root" path="/" handler={Master}>
  <Route name="composer" handler={Composer}>
    <Route name="browse" path="browse" handler={Browse}>
      <Route name="browse-details" path=":id" handler={BrowseDetails}/>
    </Route>
    <Route name="create" handler={Create} />
    <Route name="arrange" handler={Arrange} />
    <Redirect to="browse" />
  </Route>
</Route>

mjackson avatar Jan 23 '15 16:01 mjackson

It's just a way to declare a route that will be rendered when none of its siblings match.

You're describing <NotFoundRoute>. <DefaultRoute> is rendered when parent route matches exactly. Your point still stands.

agundermann avatar Jan 23 '15 17:01 agundermann

Ah, yes. Please listen to @taurose and ignore me when I make GH comments early in the morning :)

mjackson avatar Jan 23 '15 18:01 mjackson

@mjackson thanks that works nicely!

0x80 avatar Jan 25 '15 15:01 0x80