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

Update descendant routes warning message #8202

Merged
merged 1 commit into from Dec 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
44 changes: 41 additions & 3 deletions packages/react-router/__tests__/descendant-routes-warning-test.tsx
Expand Up @@ -52,9 +52,47 @@ describe("Descendant <Routes>", () => {
});

expect(consoleWarn).toHaveBeenCalledTimes(1);
expect(consoleWarn).toHaveBeenCalledWith(
expect.stringContaining("child routes will never render")
);

expect(consoleWarn.mock.calls[0][0])
.toMatch(`You rendered descendant <Routes> (or called \`useRoutes()\`) at "/courses/react" (under <Route path="react">) but the parent route path has no trailing "*". This means if you navigate deeper, the parent won't match anymore and therefore the child routes will never render.

Please change the parent <Route path="react"> to <Route path="react/*">.`);
});
});

describe("when the parent route path does not have a trailing * and is the root", () => {
it("warns once when you visit the parent route and only shows a hint like *", () => {
function ReactCourses() {
return (
<div>
<h1>React</h1>

<Routes>
<Route
path="/react-fundamentals"
element={<h1>React Fundamentals</h1>}
/>
</Routes>
</div>
);
}

TestRenderer.act(() => {
TestRenderer.create(
<MemoryRouter initialEntries={["/"]}>
<Routes>
<Route path="/" element={<ReactCourses />} />
</Routes>
</MemoryRouter>
);
});

expect(consoleWarn).toHaveBeenCalledTimes(1);

expect(consoleWarn.mock.calls[0][0])
.toMatch(`You rendered descendant <Routes> (or called \`useRoutes()\`) at "/" (under <Route path="/">) but the parent route path has no trailing "*". This means if you navigate deeper, the parent won't match anymore and therefore the child routes will never render.

Please change the parent <Route path="/"> to <Route path="*">.`);
});
});

Expand Down
2 changes: 1 addition & 1 deletion packages/react-router/index.tsx
Expand Up @@ -610,7 +610,7 @@ export function useRoutes(
`deeper, the parent won't match anymore and therefore the child ` +
`routes will never render.\n\n` +
`Please change the parent <Route path="${parentPath}"> to <Route ` +
`path="${parentPath}/*">.`
`path="${parentPath === "/" ? "*" : `${parentPath}/*`}">.`
);
}

Expand Down