Skip to content

Commit 2b88aa2

Browse files
committed
fix: get CompatRoute to match partially
1 parent 0188810 commit 2b88aa2

File tree

2 files changed

+23
-16
lines changed

2 files changed

+23
-16
lines changed

packages/react-router-dom-v5-compat/README.md

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -256,40 +256,46 @@ BAM 💥 This entire branch of your UI is migrated to v6!
256256

257257
Once your deepest `Switch` components are converted, go up to their parent `<Switch>` and repeat the process. Keep doing this all the way up the tree until all components are migrated to v6 APIs.
258258

259-
If you have `<Routes>` rendered deeper in the tree of a `<Route>`, you'll need to use a **splat path** so that those deeper URLs continue to match.
259+
When you convert a `<Switch>` to `<Routes>` that has descendant `<Routes>` deeper in its tree, there are a couple things you need to do in both places for everything to continue matching correctly.
260260

261261
👉️ Add splat paths to any `<Route>` with a **descendant** `<Routes>`
262262

263263
```diff
264-
function Ancestor() {
264+
function Root() {
265265
return (
266-
- <Switch>
267-
- <CompatRoute path="/projects" component={Descendant} />
268-
- </Switch>
269-
+ <Routes>
270-
+ {/* Note the trailing "*" in the path! */}
271-
+ <Route path="/projects/*" element={<Descendant />} />
272-
+ </Routes>
266+
<Routes>
267+
- <Route path="/projects" element={<Projects />} />
268+
+ <Route path="/projects/*" element={<Projects />} />
269+
</Routes>
273270
);
274271
}
272+
```
273+
274+
This ensures deeper URLs like `/projects/123` continue to match that route. Note that this isn't needed if the route doesn't have any descendant `<Routes>`.
275275

276-
// This one was already migrated earlier but it won't match anymore
277-
// if you don't add the "*" when you change <Switch> to <Routes>
278-
function Descendant() {
276+
👉 Convert route paths from absolute to relative paths
277+
278+
```diff
279+
- function Projects(props) {
280+
- let { match } = props
281+
function Projects() {
279282
return (
280283
<div>
281284
<h1>Projects</h1>
282285
<Routes>
283-
<Route path="activity" element={<ProjectsActivity />} />
284-
<Route path=":projectId" element={<Project />} />
285-
<Route path=":projectId/edit" element={<EditProject />} />
286+
- <Route path={match.path + "/activity"} element={<ProjectsActivity />} />
287+
- <Route path={match.path + "/:projectId"} element={<Project />} />
288+
- <Route path={match.path + "/:projectId/edit"} element={<EditProject />} />
289+
+ <Route path="activity" element={<ProjectsActivity />} />
290+
+ <Route path=":projectId" element={<Project />} />
291+
+ <Route path=":projectId/edit" element={<EditProject />} />
286292
</Routes>
287293
</div>
288294
);
289295
}
290296
```
291297

292-
The splat is only needed for routes with **descendant routes** in their tree.
298+
Usually descendant Switch (and now Routes) were using the ancestor `match.path` to build their entire path. When the ancestor Switch is converted to `<Routes>` you no longer need to do this this manually, it happens automatically. Also, if you don't change them to relative paths, they will no longer match, so you need to do this step.
293299

294300
### 6) Remove the compatibility package!
295301

packages/react-router-dom-v5-compat/lib/components.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { Router, Routes, Route } from "../react-router-dom";
1515
// but not worried about that for now.
1616
export function CompatRoute(props: any) {
1717
let { path } = props;
18+
if (!props.exact) path += "/*";
1819
return (
1920
<Routes>
2021
<Route path={path} element={<RouteV5 {...props} />} />

0 commit comments

Comments
 (0)