Skip to content

Commit

Permalink
Allow descendant <Routes> to receive all params
Browse files Browse the repository at this point in the history
Fixes #8073
  • Loading branch information
mjackson committed Oct 5, 2021
1 parent e3cc485 commit 16214b2
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
34 changes: 34 additions & 0 deletions packages/react-router/__tests__/descendant-routes-params-test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import * as React from "react";
import { create as createTestRenderer } from "react-test-renderer";
import { MemoryRouter as Router, Routes, Route, useParams } from "react-router";

describe("Descendant <Routes>", () => {
it("receive all params from ancestor <Routes>", () => {
function Message() {
return <p>The params are {JSON.stringify(useParams())}</p>;
}

function User() {
return (
<Routes>
<Route path="messages/:messageId" element={<Message />} />
</Routes>
);
}

let renderer = createTestRenderer(
<Router initialEntries={["/users/mj/messages/123"]}>
<Routes>
<Route path="users/:userId/*" element={<User />} />
</Routes>
</Router>
);

expect(renderer.toJSON()).toMatchInlineSnapshot(`
<p>
The params are
{"userId":"mj","*":"messages/123","messageId":"123"}
</p>
`);
});
});
5 changes: 3 additions & 2 deletions packages/react-router/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -653,6 +653,7 @@ export function useRoutes(
matches &&
matches.map(match =>
Object.assign({}, match, {
params: Object.assign({}, parentParams, match.params),
pathname: joinPaths([parentPathnameStart, match.pathname])
})
)
Expand Down Expand Up @@ -692,10 +693,10 @@ export function createRoutesFromChildren(
}

let route: RouteObject = {
path: element.props.path,
caseSensitive: element.props.caseSensitive,
element: element.props.element,
index: element.props.index,
element: element.props.element
path: element.props.path
};

if (element.props.children) {
Expand Down

0 comments on commit 16214b2

Please sign in to comment.