@modern-js/main-doc 0.0.0-nightly-20250504160308 → 0.0.0-nightly-20250506160336
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -88,3 +88,73 @@ export default () => {
|
|
|
88
88
|
`<Outlet>` is a new API in React Router 6. For details, see [Outlet](https://reactrouter.com/en/main/components/outlet#outlet).
|
|
89
89
|
|
|
90
90
|
:::
|
|
91
|
+
|
|
92
|
+
## Upgrading to React Router v7
|
|
93
|
+
|
|
94
|
+
React Router v7 reduces bundle size (approximately 15% smaller) compared to React Router v6, provides a more efficient route matching algorithm, and offers better support for React 19 and TypeScript. There are very few breaking changes compared to React Router v6, and Modern.js has made both versions compatible, allowing for a seamless upgrade by simply installing and registering the appropriate plugin.
|
|
95
|
+
|
|
96
|
+
:::info
|
|
97
|
+
|
|
98
|
+
For more changes from React Router v6 to React Router v7, check the [documentation](https://reactrouter.com/upgrading/v6#upgrade-to-v7)
|
|
99
|
+
|
|
100
|
+
:::
|
|
101
|
+
|
|
102
|
+
### Requirements
|
|
103
|
+
|
|
104
|
+
React Router v7 has certain environment requirements:
|
|
105
|
+
|
|
106
|
+
- Node.js 20+
|
|
107
|
+
- React 18+
|
|
108
|
+
- React DOM 18+
|
|
109
|
+
|
|
110
|
+
### Install the Plugin
|
|
111
|
+
|
|
112
|
+
First, install the Modern.js React Router v7 plugin:
|
|
113
|
+
|
|
114
|
+
```bash
|
|
115
|
+
pnpm add @modern-js/plugin-router-v7
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
### Configure the Plugin
|
|
119
|
+
|
|
120
|
+
Register the plugin in `modern.config.ts`:
|
|
121
|
+
|
|
122
|
+
```ts title="modern.config.ts"
|
|
123
|
+
import { routerPlugin } from '@modern-js/plugin-router-v7';
|
|
124
|
+
|
|
125
|
+
export default {
|
|
126
|
+
runtime: {
|
|
127
|
+
router: true,
|
|
128
|
+
},
|
|
129
|
+
plugins: [routerPlugin()],
|
|
130
|
+
};
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
### Code Changes
|
|
134
|
+
|
|
135
|
+
In React Router v7, you no longer need to use the `defer` API; you can directly return data in the data loader:
|
|
136
|
+
|
|
137
|
+
```ts title="routes/page.data.ts"
|
|
138
|
+
import { defer } from '@modern-js/runtime/router';
|
|
139
|
+
|
|
140
|
+
export const loader = async ({ params }) => {
|
|
141
|
+
// Recommended v7 style
|
|
142
|
+
const user = fetchUser(params.id)
|
|
143
|
+
return { user };
|
|
144
|
+
|
|
145
|
+
// v6 style, still compatible with Modern.js
|
|
146
|
+
return defer({ data: 'hello' });
|
|
147
|
+
};
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
React Router v7 has also deprecated the `json` API:
|
|
151
|
+
|
|
152
|
+
```ts title="routes/page.data.ts"
|
|
153
|
+
export const loader = async ({ params }) => {
|
|
154
|
+
// Recommended v7 style
|
|
155
|
+
return { data: 'hello' };
|
|
156
|
+
|
|
157
|
+
// v6 style, still compatible with Modern.js
|
|
158
|
+
return json({ data: 'hello' });
|
|
159
|
+
};
|
|
160
|
+
```
|
|
@@ -495,6 +495,78 @@ import Motivation from '@site-docs/components/convention-routing-motivation';
|
|
|
495
495
|
|
|
496
496
|
<Motivation />
|
|
497
497
|
|
|
498
|
+
## 升级到 react-router v7
|
|
499
|
+
|
|
500
|
+
React Router v7 相比 React Router v6 减少了包体积(小约 15%),提供了更高效的路由匹配算法,对 React 19 和 TypeScript 也提供了更好的支持,
|
|
501
|
+
相比 React Router v6 breaking change 非常少,同时 Modern.js 也对两个版本做了兼容,只需在项目中安装并注册相应的插件即可无缝升级。
|
|
502
|
+
|
|
503
|
+
:::info
|
|
504
|
+
|
|
505
|
+
更多 react router v6 到 react router v7 的变更,请查看[文档](https://reactrouter.com/upgrading/v6#upgrade-to-v7)
|
|
506
|
+
|
|
507
|
+
:::
|
|
508
|
+
|
|
509
|
+
### 环境要求
|
|
510
|
+
|
|
511
|
+
React Router v7 对环境有一定要求:
|
|
512
|
+
|
|
513
|
+
- Node.js 20+
|
|
514
|
+
- React 18+
|
|
515
|
+
- React DOM 18+
|
|
516
|
+
|
|
517
|
+
### 安装插件
|
|
518
|
+
|
|
519
|
+
首先,安装 Modern.js 的 React Router v7 插件:
|
|
520
|
+
|
|
521
|
+
```bash
|
|
522
|
+
pnpm add @modern-js/plugin-router-v7
|
|
523
|
+
```
|
|
524
|
+
|
|
525
|
+
### 配置插件
|
|
526
|
+
|
|
527
|
+
在 `modern.config.ts` 中注册插件:
|
|
528
|
+
|
|
529
|
+
```ts title="modern.config.ts"
|
|
530
|
+
import { routerPlugin } from '@modern-js/plugin-router-v7';
|
|
531
|
+
|
|
532
|
+
export default {
|
|
533
|
+
runtime: {
|
|
534
|
+
router: true,
|
|
535
|
+
},
|
|
536
|
+
plugins: [routerPlugin()],
|
|
537
|
+
};
|
|
538
|
+
```
|
|
539
|
+
|
|
540
|
+
### 修改代码
|
|
541
|
+
|
|
542
|
+
在 react router v7 中,不需要再使用 `defer` API 了,直接在 data loader 中返回数据即可:
|
|
543
|
+
|
|
544
|
+
```ts title="routes/page.data.ts"
|
|
545
|
+
import { defer } from '@modern-js/runtime/router';
|
|
546
|
+
|
|
547
|
+
export const loader = async ({ params }) => {
|
|
548
|
+
// 推荐的 v7 风格
|
|
549
|
+
const user = fetchUser(params.id)
|
|
550
|
+
return { user };
|
|
551
|
+
|
|
552
|
+
// v6 风格,Modern.js 做了兼容,仍然可以继续使用
|
|
553
|
+
return defer({ data: 'hello' });
|
|
554
|
+
};
|
|
555
|
+
```
|
|
556
|
+
|
|
557
|
+
react router v7 同样废弃了 `json` API:
|
|
558
|
+
|
|
559
|
+
```ts title="routes/page.data.ts"
|
|
560
|
+
export const loader = async ({ params }) => {
|
|
561
|
+
// 推荐的 v7 风格
|
|
562
|
+
return { data: 'hello' };
|
|
563
|
+
|
|
564
|
+
// v6 风格,Modern.js 做了兼容,仍然可以继续使用
|
|
565
|
+
return json({ data: 'hello' });
|
|
566
|
+
};
|
|
567
|
+
```
|
|
568
|
+
|
|
569
|
+
|
|
498
570
|
## 常见问题
|
|
499
571
|
|
|
500
572
|
1. 为什么要提供 `@modern-js/runtime/router` 来导出 React Router API ?
|
package/package.json
CHANGED
|
@@ -15,14 +15,14 @@
|
|
|
15
15
|
"modern",
|
|
16
16
|
"modern.js"
|
|
17
17
|
],
|
|
18
|
-
"version": "0.0.0-nightly-
|
|
18
|
+
"version": "0.0.0-nightly-20250506160336",
|
|
19
19
|
"publishConfig": {
|
|
20
20
|
"registry": "https://registry.npmjs.org/",
|
|
21
21
|
"access": "public"
|
|
22
22
|
},
|
|
23
23
|
"dependencies": {
|
|
24
24
|
"mermaid": "^11.4.1",
|
|
25
|
-
"@modern-js/sandpack-react": "0.0.0-nightly-
|
|
25
|
+
"@modern-js/sandpack-react": "0.0.0-nightly-20250506160336"
|
|
26
26
|
},
|
|
27
27
|
"devDependencies": {
|
|
28
28
|
"@rspress/shared": "1.43.11",
|