Skip to content

Commit bef3166

Browse files
liuhanquhanquliu
andauthored
Fix: use outlet directly if route without element (#8497)
Co-authored-by: hanquliu <[email protected]>
1 parent 921d270 commit bef3166

File tree

2 files changed

+85
-1
lines changed

2 files changed

+85
-1
lines changed

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

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

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)