Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nested IndexRoute not working #1950

Closed
pmatev opened this issue Sep 15, 2015 · 16 comments
Closed

Nested IndexRoute not working #1950

pmatev opened this issue Sep 15, 2015 · 16 comments

Comments

@pmatev
Copy link

pmatev commented Sep 15, 2015

I have this route setup, where I need nested IndexRoutes

<Route path="/admin" component={App}>
    <IndexRoute component={HomeBody}>
       <IndexRoute component={Overview}/>
    </IndexRoute>
    // more routes...
</Route>

But the inner IndexRoute doesn't ever seem to resolve.

@ryanflorence
Copy link
Member

That doesn't make sense, index routes are leaf nodes, can't be nested.

@raveclassic
Copy link

@ryanflorence Sorry about writing to the closed issue.
You're saying that nesting IndexRoutes does not make sense, but why not?
What if I need complex default structure without any redirects just at my root ('/' path)?
As long as new api provides component attributes to every route why do you not allow to nest default route component via children?

Like this: (Here App and Authorized components just render their children)

<Route path="/" component={App}>
    <IndexRoute component={Authorized}>
        <IndexRoute component={Feed}/>
    </IndexRoute>
    <Route path="login" component={Login}/>
</Route>

Another way to accomplish the desired behavior would be using just Route components without path specified and inheriting it from parent but that is not working.
Like this:

<Route path="/" component={App}>
    <Route component={Authorized}>
        <Route component={Feed}/>
    </Route>
    <Route path="login" component={Login}/>
</Route>

But actually this is just the same as using IndexRoute.

@taion
Copy link
Contributor

taion commented Nov 30, 2015

<Route path="/" component={App}>
    <Route component={Authorized}>
        <IndexRoute component={Feed} />
    </Route>
    <Route path="login" component={Login}/>
</Route>

Does that make sense? We can follow up on Reactiflux or Stack Overflow - the issue tracker is not great for this sort of Q&A thing.

@ghempton
Copy link
Contributor

This seems like a valid issue. @taion's solution does not solve the problem.

@taion
Copy link
Contributor

taion commented Dec 22, 2015

Elaborate?

@ghempton
Copy link
Contributor

Maybe I am just confusing myself, but the discrepancies between the following cases are not intuitive:

This works:

<Route component={App}>
    <Route path="/ component={Authorized}>
        <IndexRoute component={Feed} />
        ...
    </Route>
    <Route path="/" component={LoginLayout}>
        <Route path="login" component={Login} />
        ...
    </Route>
</Route>

This does not work:

<Route path="/" component={App}>
    <Route path="/" component={Authorized}>
        <IndexRoute component={Feed} />
        ...
    </Route>
    <Route path="/" component={LoginLayout}>
        <Route path="login" component={Login} />
        ...
    </Route>
</Route>

This also does not work, but I think semantically captures what I am trying to do and should be supported:

<Route path="/" component={App}>
    <IndexRoute component={Authorized}>
        <IndexRoute component={Feed} />
        ...
    </Route>
    <Route path="/" component={LoginLayout}>
        <Route path="login" component={Login} />
        ...
    </Route>
</Route>

@taion
Copy link
Contributor

taion commented Dec 22, 2015

Should be

<Route path="/" component={App}>
    <Route component={Authorized}>
        <IndexRoute component={Feed} />
        ...
    </Route>
    <Route component={LoginLayout}>
        <Route path="login" component={Login} />
        ...
    </Route>
</Route>

@timdorr
Copy link
Member

timdorr commented Dec 22, 2015

Your second example doesn't work because it's trying to express // as a path to navigate to. Each path, even if duplicated, becomes a node in the route tree, separated by slashes (or being a slash itself if there is nothing else in the path).

You're really close. Here's what you need:

<Route path="/" component={App}>
    <Route component={Authorized}>
        <IndexRoute component={Feed} />
    </Route>
    <Route component={LoginLayout}>
        <Route path="login" component={Login} />
        <Route path="forgot_password" component={ForgotPassword} />
    </Route>
</Route>

Edit: What @taion said.

@ghempton
Copy link
Contributor

ah makes sense, thanks guys.

@nikdo
Copy link

nikdo commented Jan 16, 2016

I believe I have found a case where nested routes for IndexRoute actually make sense. Please correct me if I am wrong.

@ryanflorence
Copy link
Member

I just commented over there.

@nikdo
Copy link

nikdo commented Jan 18, 2016

@taion @ryanflorence Thank you both for clarification.

@benwiley4000
Copy link
Contributor

@timdorr I appreciate (and I'm using) the solution you gave. However it seems somewhat non-intuitive to me that a Route could fall through to an IndexRoute that is not an immediate child. It seems odd that the grandparent would be able to figure that one of its path-less children has an IndexRoute, so it should go ahead and fall through. Am I missing something conceptually about React Router?

@taion
Copy link
Contributor

taion commented Jun 17, 2016

It lets you handle cases like #3326 (comment) cleanly.

@benwiley4000
Copy link
Contributor

@taion aha, interesting discussion. Thanks for sharing.

@taion
Copy link
Contributor

taion commented Jun 17, 2016

Yeah, so it's nothing conceptual and at some level it's a bit sloppy, but in practice it's really convenient for certain kinds of route definitions.

@lock lock bot locked as resolved and limited conversation to collaborators Jan 21, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants