Skip to content

Commit 9de81d2

Browse files
chaancemjackson
andauthored
Make all params available to nested routes + parents (#7963)
* feat: Make all params available to nested routes (#7960) * add test case * update type imports * bump file size limits Co-authored-by: Michael Jackson <[email protected]>
1 parent 7037663 commit 9de81d2

File tree

3 files changed

+37
-8
lines changed

3 files changed

+37
-8
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,13 +76,13 @@
7676
"none": "6 kB"
7777
},
7878
"build/react-router/umd/react-router.production.min.js": {
79-
"none": "7 kB"
79+
"none": "8 kB"
8080
},
8181
"build/react-router-dom/react-router-dom.production.min.js": {
8282
"none": "3 kB"
8383
},
8484
"build/react-router-dom/umd/react-router-dom.production.min.js": {
85-
"none": "7 kB"
85+
"none": "8 kB"
8686
}
8787
},
8888
"prettier": {

packages/react-router/__tests__/useParams-test.tsx

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,4 +193,33 @@ describe("useParams", () => {
193193
);
194194
});
195195
});
196+
197+
describe("when the params match in a child route", () => {
198+
it("renders params in the parent", () => {
199+
let params: Record<string, string> = {};
200+
function Blog() {
201+
params = useParams();
202+
return <h1>{params.slug}</h1>;
203+
}
204+
205+
function BlogPost() {
206+
return null;
207+
}
208+
209+
createTestRenderer(
210+
<Router initialEntries={["/blog/react-router"]}>
211+
<Routes>
212+
<Route path="/blog" element={<Blog />}>
213+
<Route path=":slug" element={<BlogPost />} />
214+
</Route>
215+
</Routes>
216+
</Router>
217+
);
218+
219+
expect(typeof params).toBe("object");
220+
expect(params).toMatchObject({
221+
slug: "react-router"
222+
});
223+
});
224+
});
196225
});

packages/react-router/index.tsx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as React from "react";
2-
import {
3-
Action,
2+
import { Action, createMemoryHistory, parsePath } from "history";
3+
import type {
44
Blocker,
55
History,
66
InitialEntry,
@@ -10,9 +10,7 @@ import {
1010
Path,
1111
State,
1212
To,
13-
Transition,
14-
createMemoryHistory,
15-
parsePath
13+
Transition
1614
} from "history";
1715

1816
const readOnly: <T>(obj: T) => Readonly<T> = __DEV__
@@ -553,13 +551,15 @@ function useRoutes_(
553551
}
554552

555553
// Otherwise render an element.
554+
let allParams: Params = {};
556555
let element = matches.reduceRight((outlet, { params, pathname, route }) => {
556+
allParams = { ...allParams, ...params };
557557
return (
558558
<RouteContext.Provider
559559
children={route.element}
560560
value={{
561561
outlet,
562-
params: readOnly<Params>({ ...parentParams, ...params }),
562+
params: readOnly<Params>({ ...parentParams, ...allParams }),
563563
pathname: joinPaths([basenameForMatching, pathname]),
564564
basename,
565565
route

0 commit comments

Comments
 (0)