Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: use outlet directly if route without element #8497

Merged
merged 2 commits into from
Feb 25, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 86 additions & 1 deletion packages/react-router/__tests__/useOutlet-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import {
Routes,
Route,
useOutlet,
useOutletContext
useOutletContext,
Outlet
} from "react-router";

describe("useOutlet", () => {
Expand Down Expand Up @@ -268,4 +269,88 @@ describe("useOutlet", () => {
`);
});
});

describe("when child route without element prop", () => {
it("returns nested route element", () => {
function Layout() {
return useOutlet();
}

let renderer: TestRenderer.ReactTestRenderer;
TestRenderer.act(() => {
renderer = TestRenderer.create(
<MemoryRouter initialEntries={["/users/profile"]}>
<Routes>
<Route path="/" element={<Layout />}>
<Route path="users">
<Route path="profile" element={<h1>Profile</h1>} />
</Route>
</Route>
</Routes>
</MemoryRouter>
);
});

expect(renderer.toJSON()).toMatchInlineSnapshot(`
<h1>
Profile
</h1>
`);
});

it("returns the context", () => {
function Layout() {
return useOutlet(["Mary", "Jane", "Michael"]);
}

function Profile() {
let outletContext = useOutletContext<string[]>();

return (
<div>
<h1>Profile</h1>
<ul>
{outletContext.map(name => (
<li key={name}>{name}</li>
))}
</ul>
</div>
);
}

let renderer: TestRenderer.ReactTestRenderer;
TestRenderer.act(() => {
renderer = TestRenderer.create(
<MemoryRouter initialEntries={["/users/profile"]}>
<Routes>
<Route path="/" element={<Layout />}>
<Route path="users">
<Route path="profile" element={<Profile />} />
</Route>
</Route>
</Routes>
</MemoryRouter>
);
});

expect(renderer.toJSON()).toMatchInlineSnapshot(`
<div>
<h1>
Profile
</h1>
<ul>
<li>
Mary
</li>
<li>
Jane
</li>
<li>
Michael
</li>
</ul>
</div>
`);
});
});
});
2 changes: 1 addition & 1 deletion packages/react-router/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1070,7 +1070,7 @@ function _renderMatches(
return (
<RouteContext.Provider
children={
match.route.element !== undefined ? match.route.element : <Outlet />
match.route.element !== undefined ? match.route.element : outlet
}
value={{
outlet,
Expand Down