diff --git a/package.json b/package.json index b86016b84e..41d52c7225 100644 --- a/package.json +++ b/package.json @@ -76,13 +76,13 @@ "none": "6 kB" }, "build/react-router/umd/react-router.production.min.js": { - "none": "7 kB" + "none": "8 kB" }, "build/react-router-dom/react-router-dom.production.min.js": { "none": "3 kB" }, "build/react-router-dom/umd/react-router-dom.production.min.js": { - "none": "7 kB" + "none": "8 kB" } }, "prettier": { diff --git a/packages/react-router/__tests__/useParams-test.tsx b/packages/react-router/__tests__/useParams-test.tsx index 5b686bef29..d785edf661 100644 --- a/packages/react-router/__tests__/useParams-test.tsx +++ b/packages/react-router/__tests__/useParams-test.tsx @@ -193,4 +193,33 @@ describe("useParams", () => { ); }); }); + + describe("when the params match in a child route", () => { + it("renders params in the parent", () => { + let params: Record = {}; + function Blog() { + params = useParams(); + return

{params.slug}

; + } + + function BlogPost() { + return null; + } + + createTestRenderer( + + + }> + } /> + + + + ); + + expect(typeof params).toBe("object"); + expect(params).toMatchObject({ + slug: "react-router" + }); + }); + }); }); diff --git a/packages/react-router/index.tsx b/packages/react-router/index.tsx index 3bc774f678..4acf094540 100644 --- a/packages/react-router/index.tsx +++ b/packages/react-router/index.tsx @@ -1,6 +1,6 @@ import * as React from "react"; -import { - Action, +import { Action, createMemoryHistory, parsePath } from "history"; +import type { Blocker, History, InitialEntry, @@ -10,9 +10,7 @@ import { Path, State, To, - Transition, - createMemoryHistory, - parsePath + Transition } from "history"; const readOnly: (obj: T) => Readonly = __DEV__ @@ -553,13 +551,15 @@ function useRoutes_( } // Otherwise render an element. + let allParams: Params = {}; let element = matches.reduceRight((outlet, { params, pathname, route }) => { + allParams = { ...allParams, ...params }; return ( ({ ...parentParams, ...params }), + params: readOnly({ ...parentParams, ...allParams }), pathname: joinPaths([basenameForMatching, pathname]), basename, route