react-router: [Bug]: Relative navigation issue while navigating from "index" route: ".." doesn't works, I must use "../.." to go back
What version of React Router are you using?
6.0.2
Steps to Reproduce
<Some other routes>
....
<Route path='/pages'>
<Route index element={<Pages />} />
<Route path=':pageId' element={<Item />} />
</Route>
Navigation via ‘…’ from Pages with “index” doesn’t works but it is ok for Item I should use ‘…/…’ for the same result as ‘…’
Expected Behavior
Should navigate level up via ‘…’ from index route And it would be nice to have option with nested routes that is used only to create url hierarchy for relative navigation usage, but without children and parrents ui rendering
Actual Behavior
doesnt navigate from index page via ‘…’
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Reactions: 6
- Comments: 21 (11 by maintainers)
The workaround is to use
resolvePath
that has no relation to<Route>
tree:I experience this problem and it makes kind of no sense how these work. I actually expect it to go up a route in the tree as opposed to just removing one segment (although in this particular instance it’s the same). Here are my routes:
Here’s the list of routes I have with the desired
to=".."
behavior:So the silly behavior comes from how the
index
routes are handled - they’re treated as children of the parent route instead of being treated as the parent route itself.Basically what I’m saying is, I think
to=".."
inside index routes should be treated asto="../.."
for proper predictability. Otherwise, there’s no workaround for this right now since I can’t just render the element in the parent route, since that’ll render it for all child routes; and I can’t move everything a level up since that’ll break navigation for the rest of the child routes.Upd: I realize that’s what everyone is saying, kinda got lost in the point while trying to make it. But the conclusion stands - index routes should ignore one parent in the tree when going back.
I think it is because
..
means navigate one<Route>
up in the routes tree and not one path segment (directory) up. Consider this example:Both
/test/a/b/c
and/test/a
are redirected to/test
because one route up in the tree is<Route path='/test'>
that is really weird. I can’t find this in documentation.This has been fixed in #8697 / #8954. Routes that do not provide any path should not contribute to relative pathing logic - should be released in 6.4.0
Update: One more case handled in #8985
Whew … this is a tough one but I get it.
The question is:
..
means “up the route hierarchy”I think everybody is right that (2) is probably better. So the explanation would be
Seems easy enough to explain and both index and pathless layout routes don’t have any paths.
What’s the point of life if we can’t be silly sometimes?
Is there any way/workaround to receive the preivous behaviour? I’ve got a use-case where the previous behaviour was really beneficial around optional props.
In V5 we had a route with an optional parameter (
options/:option?
), which we now render like this:We were hoping that in
ListPage
we could define our routes with a simple<Link to="../<option id>">Option</Link>
. But sinceindex
routes are ignored, the routing ends up leavingoptions
.This is fixed in the above mentioned PRs - should be included in the next
6.4.0
prerelease 👍This is REALLY annoying with index routes and used to work in beta.
Imagine scenario
Seems that a route match gets generated for both the parent route and the nested index route. Matches array of RouteContext:
Reproducible demo https://stackblitz.com/edit/github-1iyzlr?file=src%2FApp.tsx
Thus navigating to
..
in my case, would pop the index page out of the match stack, and then take the user to that address. But that ends up being the link to the nesting “parent” page@gowthamvbhat, thanks for pointing out that. As for me, it is better to write
unlike <a href> that goes up by path segments
.