-
-
Notifications
You must be signed in to change notification settings - Fork 10.3k
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
[Bug]: Relative navigation issue while navigating from "index" route: ".." doesn't works, I must use "../.." to go back #8350
Comments
I'm struggling w/ the same issue, but I think the root cause is because the "base" url should contains a trailing slash, otherwise the history dependency will compute the relative path wrong. The problems appears to be in the In the line 30, the algorithm just consider everything in the same level as a filename string w/o check if it is really a filename string (file.ext). |
I think it is because function NavigateUp() {
return <Navigate to='..' />;
}
<Route path='/test'>
<Route path=':a/:b/:c' element={<NavigateUp />} />
<Route path=':a' element={<NavigateUp />} />
</Route> Both |
@mrlika This is discussed here - upgrading to v6. |
@gowthamvbhat, thanks for pointing out that. As for me, it is better to write |
The workaround is to use const navigate = useNavigate();
navigate(resolvePath('..', window.location.pathname));
// or
<Link to={resolvePath('..', window.location.pathname)} />
// or
<Navigate to={resolvePath('..', window.location.pathname)} /> |
Seems that a route match gets generated for both the parent route and the nested index route.
Reproducible demo https://stackblitz.com/edit/github-1iyzlr?file=src%2FApp.tsx Thus navigating to |
It seems that this is as designed I guess that technically this is "correct" if the documentation says that .. traverses up by one "Route". Tough I feel like the index routes should maybe be a special case to the rule. |
This is REALLY annoying with index routes and used to work in beta. Imagine scenario <Route path=":id/">
<Route index element={<Link to="../../sibling" />} />
....
</Route> |
I have also experienced the bug. This is also a breaking change coming from reach router. It should be fixed or specified in the doc :) |
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: <Route path="/settings">
<Route index element={<SettingsRoute />} />
<Route
path="account"
element={<SettingsRoute page="account" />}
/>
<Route path="team">
<Route index element={<SettingsRoute page="team" />} />
<Route
path="create"
element={<CreateUserRoute />}
/>
<Route path=":id">
<Route index element={<EditUserRoute />} />
<Route
path="history"
element={<UserActivityHistoryRoute />}
/>
</Route>
</Route>
</Route> Here's the list of routes I have with the desired
So the silly behavior comes from how the Basically what I'm saying is, I think 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. |
Whew ... this is a tough one but I get it. The question is:
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? |
Trouble is it's a breaking change technically :\ Gonna see if there's a way to make it opt-in. |
I think |
@kiliman that would definitely be a breaking change. I actually based my app's entire navigation on the fact that |
@kiliman I wish I had a link or something to send to you, but we labored over that behavior for months (literally) with dozens of scenarios and are confident traversing the route hierarchy is preferred. The only question here is do we skip routes that don't add any path segments. |
EDIT: fixed typo and added more examples. Ha no problem. I would probably just create a helper function then to navigate relative paths. Trailing backslash means you're in a folder. Non-trailing means current route is a "file".
It even handles search params properly. If you provide them, it will overwrite existing search for current location. function getRelativePath(location: Location, relativePath: string) {
const base = new URL(`http://dummy${location.pathname}${location.search}`)
const relative = new URL(relativePath, base)
return `${relative.pathname}${relative.search}`
}
// path = /parent/child/?a=1
const location = useLocation()
const relative = getRelativePath(location, '..?b=2')
// relative = /parent/?b=2 |
This is fixed in the above mentioned PRs - should be included in the next |
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 ( <Route path="options">
<Route index element={<ListPage />}/>
<Route path=":option" element={<ListPage/>}/>
</Route> We were hoping that in |
In your case I would probably just use the presence of the let params = useParams();
let to = params.option ? ".." : ".";
...
<Link to={to}>Option</Link> We have been chatting a bit about optional params recently and I think they'll be on the roadmap once we finish getting Remix on top of React Router 6.4, so keep an eye on #8381 as we'll post any updates over there if/when we move forward on that front. |
EDIT: I think people who were confused with needing to use "../.." with index routes, were expected to use path based routing. So maybe it's better to revert to the old behavior, but switch to path based routing by default. @kirkobyte, @brophdawg11: I stumbled into exactly the same issue. I have a module and all my components link relative from there. But once I use an index route, all links break. Which means index routes can't be really used anymore in a meaningful way, because they break relative links without any way of fixing it (there isn't even an opt-in switch on the Route). This new "fixed" behavior is highly illogical for me. This "new feature" already cost me hours of my productivity, because it's really hard to figure out what "parent route" really means. Route should have a flag like <Route path="module">
<Route index element={<FeatureA/>}/> {/* which breaks all the links, no way to solve it, except handling it inside feature a */}
<Route path="feature-a" element={<FeatureA/>}/>
<Route path="feature-b" element={<FeatureB/>}/>
{...}
</Route> ugly workaround: <Route path="module">
<Route index element={<Naviage to='./feature-a' />}/>
<Route path="feature-a" element={<FeatureA/>}/>
<Route path="feature-b" element={<FeatureB/>}/>
{...}
</Route> |
What version of React Router are you using?
6.0.2
Steps to Reproduce
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 '..'
The text was updated successfully, but these errors were encountered: