@modern-js/main-doc 0.0.0-nightly-20240530170619 → 0.0.0-nightly-20240531170650
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.
@@ -155,6 +155,8 @@ function loader() {
|
|
155
155
|
|
156
156
|
### Error Handling
|
157
157
|
|
158
|
+
#### Basic Usage
|
159
|
+
|
158
160
|
In the `loader` function, errors can be handled by throwing an `error` or a `response`. When an error is thrown in the `loader` function, Modern.js will stop executing the code in the current `loader` and switch the front-end UI to the defined [`ErrorBoundary`](/guides/basic-features/routes#error-handling) component:
|
159
161
|
|
160
162
|
```tsx
|
@@ -182,6 +184,32 @@ const ErrorBoundary = () => {
|
|
182
184
|
export default ErrorBoundary;
|
183
185
|
```
|
184
186
|
|
187
|
+
#### Practice
|
188
|
+
|
189
|
+
In an SSR project you can control the status code of a page and display the corresponding UI by `throw response` in the data loader, as in the following example, where there is one loader for the entire routing route
|
190
|
+
|
191
|
+
For example, if there is a loader in the entire routing route that throws a response, the status code of the page will be consistent with this `response` and the UI of the page will switch to `ErrorBoundary`.
|
192
|
+
|
193
|
+
```ts
|
194
|
+
// routes/user/profile/page.data.ts
|
195
|
+
export async function loader() {
|
196
|
+
const user = await fetchUser();
|
197
|
+
if(!user){
|
198
|
+
throw new Response('The user was not found', { status: 404 });
|
199
|
+
}
|
200
|
+
return user;
|
201
|
+
}
|
202
|
+
|
203
|
+
// routes/error.tsx
|
204
|
+
import { useRouteError } from '@modern-js/runtime/router';
|
205
|
+
const ErrorBoundary = () => {
|
206
|
+
const error = useRouteError() as { data: string };
|
207
|
+
return <div className="error">{error.data}</div>;
|
208
|
+
};
|
209
|
+
|
210
|
+
export default ErrorBoundary;
|
211
|
+
```
|
212
|
+
|
185
213
|
### Get data from parent component
|
186
214
|
|
187
215
|
In many cases, child components need to access data in the parent component `loader`. You can easily get the data from the parent component using `useRouteLoaderData`:
|
@@ -155,6 +155,8 @@ export async function loader() {
|
|
155
155
|
|
156
156
|
### 错误处理
|
157
157
|
|
158
|
+
#### 基本用法
|
159
|
+
|
158
160
|
在 `loader` 函数中,可以通过 `throw error` 或者 `throw response` 的方式处理错误,当 `loader` 函数中有错误被抛出时,Modern.js 会停止执行当前 `loader` 中的代码,并将前端 UI 切换到定义的 [`ErrorBoundary`](/guides/basic-features/routes#错误处理) 组件:
|
159
161
|
|
160
162
|
```tsx
|
@@ -182,6 +184,31 @@ const ErrorBoundary = () => {
|
|
182
184
|
export default ErrorBoundary;
|
183
185
|
```
|
184
186
|
|
187
|
+
#### 实践
|
188
|
+
|
189
|
+
在 SSR 项目中你可以通过在 data loader 中 `throw response` 的方式,控制页面的状态码,展示对应的 UI,如以下示例,整条路由路线中有一个 loader
|
190
|
+
throw response,页面的状态码将与这个 `response` 保持一致,页面的 UI 也会切换为 `ErrorBoundary`:
|
191
|
+
|
192
|
+
```ts
|
193
|
+
// routes/user/profile/page.data.ts
|
194
|
+
export async function loader() {
|
195
|
+
const user = await fetchUser();
|
196
|
+
if(!user){
|
197
|
+
throw new Response('The user was not found', { status: 404 });
|
198
|
+
}
|
199
|
+
return user;
|
200
|
+
}
|
201
|
+
|
202
|
+
// routes/error.tsx
|
203
|
+
import { useRouteError } from '@modern-js/runtime/router';
|
204
|
+
const ErrorBoundary = () => {
|
205
|
+
const error = useRouteError() as { data: string };
|
206
|
+
return <div className="error">{error.data}</div>;
|
207
|
+
};
|
208
|
+
|
209
|
+
export default ErrorBoundary;
|
210
|
+
```
|
211
|
+
|
185
212
|
### 获取上层组件的数据
|
186
213
|
|
187
214
|
很多场景下,子组件需要获取到祖先组件 `loader` 中的数据,你可以通过 `useRouteLoaderData` 方便地获取到祖先组件的数据:
|
@@ -385,8 +412,6 @@ export const loader = () => {}
|
|
385
412
|
```
|
386
413
|
|
387
414
|
|
388
|
-
|
389
|
-
|
390
415
|
### 错误用法
|
391
416
|
|
392
417
|
1. `loader` 中只能返回可序列化的数据,在 SSR 环境下,`loader` 函数的返回值会被序列化为 JSON 字符串,然后在客户端被反序列化为对象。因此,`loader` 函数中不能返回不可序列化的数据(如函数)。
|
package/package.json
CHANGED
@@ -15,17 +15,17 @@
|
|
15
15
|
"modern",
|
16
16
|
"modern.js"
|
17
17
|
],
|
18
|
-
"version": "0.0.0-nightly-
|
18
|
+
"version": "0.0.0-nightly-20240531170650",
|
19
19
|
"publishConfig": {
|
20
20
|
"registry": "https://registry.npmjs.org/",
|
21
21
|
"access": "public",
|
22
22
|
"provenance": true
|
23
23
|
},
|
24
24
|
"dependencies": {
|
25
|
-
"@modern-js/sandpack-react": "0.0.0-nightly-
|
25
|
+
"@modern-js/sandpack-react": "0.0.0-nightly-20240531170650"
|
26
26
|
},
|
27
27
|
"peerDependencies": {
|
28
|
-
"@modern-js/builder-doc": "0.0.0-nightly-
|
28
|
+
"@modern-js/builder-doc": "0.0.0-nightly-20240531170650"
|
29
29
|
},
|
30
30
|
"devDependencies": {
|
31
31
|
"classnames": "^2",
|
@@ -39,8 +39,8 @@
|
|
39
39
|
"@rspress/shared": "1.18.2",
|
40
40
|
"@types/node": "^16",
|
41
41
|
"@types/fs-extra": "9.0.13",
|
42
|
-
"@modern-js/
|
43
|
-
"@modern-js/doc
|
42
|
+
"@modern-js/doc-plugin-auto-sidebar": "0.0.0-nightly-20240531170650",
|
43
|
+
"@modern-js/builder-doc": "0.0.0-nightly-20240531170650"
|
44
44
|
},
|
45
45
|
"scripts": {
|
46
46
|
"dev": "rspress dev",
|