-
-
Notifications
You must be signed in to change notification settings - Fork 10.4k
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
Fix: useResolvedPath
and useNavigate
use different logic
#8985
Changes from all commits
6bc6fa7
282454c
6707295
7f4f210
d3e215c
e70cf75
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"react-router": patch | ||
--- | ||
|
||
fix: Additional logic fixed for relative navigation from index/pathless layout routes (#8985) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,7 @@ | |
- chrisngobanh | ||
- christopherchudzicki | ||
- cvbuelow | ||
- david-crespo | ||
- edwin177 | ||
- elylucas | ||
- emzoumpo | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -179,7 +179,7 @@ describe("<Link> href", () => { | |
expect(renderer.root.findByType("a").props.href).toEqual("/inbox"); | ||
}); | ||
|
||
test('<Link to=".."> resolves relative to the parent route', () => { | ||
test('<Link to=".."> resolves relative to the parent route (ignoring the index route)', () => { | ||
let renderer: TestRenderer.ReactTestRenderer; | ||
TestRenderer.act(() => { | ||
renderer = TestRenderer.create( | ||
|
@@ -193,7 +193,7 @@ describe("<Link> href", () => { | |
); | ||
}); | ||
|
||
expect(renderer.root.findByType("a").props.href).toEqual("/inbox"); | ||
expect(renderer.root.findByType("a").props.href).toEqual("/"); | ||
}); | ||
|
||
test('<Link to=".."> with more .. segments than parent routes resolves to the root URL', () => { | ||
|
@@ -262,7 +262,7 @@ describe("<Link> href", () => { | |
expect(renderer.root.findByType("a").props.href).toEqual("/inbox"); | ||
}); | ||
|
||
test('<Link to=".."> resolves relative to the parent route', () => { | ||
test('<Link to=".."> resolves relative to the parent route (ignoring the pathless route)', () => { | ||
let renderer: TestRenderer.ReactTestRenderer; | ||
TestRenderer.act(() => { | ||
renderer = TestRenderer.create( | ||
|
@@ -278,7 +278,7 @@ describe("<Link> href", () => { | |
); | ||
}); | ||
|
||
expect(renderer.root.findByType("a").props.href).toEqual("/inbox"); | ||
expect(renderer.root.findByType("a").props.href).toEqual("/"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as above |
||
}); | ||
|
||
test('<Link to=".."> with more .. segments than parent routes resolves to the root URL', () => { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -137,6 +137,36 @@ export interface NavigateFunction { | |
(delta: number): void; | ||
} | ||
|
||
/** | ||
* When processing relative navigation we want to ignore ancestor routes that | ||
* do not contribute to the path, such that index/pathless layout routes don't | ||
* interfere. | ||
* | ||
* For example, when moving a route element into an index route and/or a | ||
* pathless layout route, relative link behavior contained within should stay | ||
* the same. Both of the following examples should link back to the root: | ||
* | ||
* <Route path="/"> | ||
* <Route path="accounts" element={<Link to=".."}> | ||
* </Route> | ||
* | ||
* <Route path="/"> | ||
* <Route path="accounts"> | ||
* <Route element={<AccountsLayout />}> // <-- Does not contribute | ||
* <Route index element={<Link to=".."} /> // <-- Does not contribute | ||
* </Route | ||
* </Route> | ||
* </Route> | ||
*/ | ||
function getPathContributingMatches(matches: RouteMatch[]) { | ||
return matches.filter( | ||
(match, index) => | ||
index === 0 || | ||
(!match.route.index && | ||
match.pathnameBase !== matches[index - 1].pathnameBase) | ||
); | ||
} | ||
|
||
/** | ||
* Returns an imperative method for changing the location. Used by <Link>s, but | ||
* may also be used by other elements to change the location. | ||
|
@@ -155,16 +185,8 @@ export function useNavigate(): NavigateFunction { | |
let { matches } = React.useContext(RouteContext); | ||
let { pathname: locationPathname } = useLocation(); | ||
|
||
// Ignore index + pathless matches | ||
let pathContributingMatches = matches.filter( | ||
(match, index) => | ||
index === 0 || | ||
(!match.route.index && | ||
match.pathnameBase !== matches[index - 1].pathnameBase) | ||
); | ||
|
||
let routePathnamesJson = JSON.stringify( | ||
pathContributingMatches.map((match) => match.pathnameBase) | ||
getPathContributingMatches(matches).map((match) => match.pathnameBase) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I had a feeling doing the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yeah it certainly worked fine, just felt a little distanced in the shared util since it's a dependency implementation detail for the individual hooks |
||
); | ||
|
||
let activeRef = React.useRef(false); | ||
|
@@ -262,7 +284,7 @@ export function useResolvedPath(to: To): Path { | |
let { pathname: locationPathname } = useLocation(); | ||
|
||
let routePathnamesJson = JSON.stringify( | ||
matches.map((match) => match.pathnameBase) | ||
getPathContributingMatches(matches).map((match) => match.pathnameBase) | ||
); | ||
|
||
return React.useMemo( | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This
..
should ignore theindex
route and thus be relative toinbox
which would resolve to the root. The incorrect nature of this test can be seen by comparing to the test above where in this same layout,<Link to=".">
resolves to/inbox
- so..
should resolve to/