Skip to content

Commit 16e60be

Browse files
author
hanquliu
committed
Use outlet directly if route without element
1 parent 29c7fc8 commit 16e60be

File tree

2 files changed

+87
-2
lines changed

2 files changed

+87
-2
lines changed

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

Lines changed: 86 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import {
55
Routes,
66
Route,
77
useOutlet,
8-
useOutletContext
8+
useOutletContext,
9+
Outlet
910
} from "react-router";
1011

1112
describe("useOutlet", () => {
@@ -268,4 +269,88 @@ describe("useOutlet", () => {
268269
`);
269270
});
270271
});
272+
273+
describe("when child route without element prop", () => {
274+
it("returns nested route element", () => {
275+
function Layout() {
276+
return useOutlet();
277+
}
278+
279+
let renderer: TestRenderer.ReactTestRenderer;
280+
TestRenderer.act(() => {
281+
renderer = TestRenderer.create(
282+
<MemoryRouter initialEntries={["/users/profile"]}>
283+
<Routes>
284+
<Route path="/" element={<Layout />}>
285+
<Route path="users">
286+
<Route path="profile" element={<h1>Profile</h1>} />
287+
</Route>
288+
</Route>
289+
</Routes>
290+
</MemoryRouter>
291+
);
292+
});
293+
294+
expect(renderer.toJSON()).toMatchInlineSnapshot(`
295+
<h1>
296+
Profile
297+
</h1>
298+
`);
299+
});
300+
301+
it("returns the context", () => {
302+
function Layout() {
303+
return useOutlet(["Mary", "Jane", "Michael"]);
304+
}
305+
306+
function Profile() {
307+
let outletContext = useOutletContext<string[]>();
308+
309+
return (
310+
<div>
311+
<h1>Profile</h1>
312+
<ul>
313+
{outletContext.map(name => (
314+
<li key={name}>{name}</li>
315+
))}
316+
</ul>
317+
</div>
318+
);
319+
}
320+
321+
let renderer: TestRenderer.ReactTestRenderer;
322+
TestRenderer.act(() => {
323+
renderer = TestRenderer.create(
324+
<MemoryRouter initialEntries={["/users/profile"]}>
325+
<Routes>
326+
<Route path="/" element={<Layout />}>
327+
<Route path="users">
328+
<Route path="profile" element={<Profile />} />
329+
</Route>
330+
</Route>
331+
</Routes>
332+
</MemoryRouter>
333+
);
334+
});
335+
336+
expect(renderer.toJSON()).toMatchInlineSnapshot(`
337+
<div>
338+
<h1>
339+
Profile
340+
</h1>
341+
<ul>
342+
<li>
343+
Mary
344+
</li>
345+
<li>
346+
Jane
347+
</li>
348+
<li>
349+
Michael
350+
</li>
351+
</ul>
352+
</div>
353+
`);
354+
});
355+
});
271356
});

packages/react-router/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1070,7 +1070,7 @@ function _renderMatches(
10701070
return (
10711071
<RouteContext.Provider
10721072
children={
1073-
match.route.element !== undefined ? match.route.element : <Outlet />
1073+
match.route.element !== undefined ? match.route.element : outlet
10741074
}
10751075
value={{
10761076
outlet,

0 commit comments

Comments
 (0)