@modern-js/main-doc 2.0.0-canary.0 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- package/.turbo/turbo-build.log +1 -1
- package/en/docusaurus-plugin-content-docs/current/apis/app/runtime/_category_.json +1 -1
- package/en/docusaurus-plugin-content-docs/current/apis/app/runtime/core/use-module-apps.md +62 -31
- package/en/docusaurus-plugin-content-docs/current/apis/app/runtime/router/router.md +174 -375
- package/en/docusaurus-plugin-content-docs/current/components/enable-bff.md +36 -0
- package/en/docusaurus-plugin-content-docs/current/components/enable-micro-frontend.md +13 -0
- package/en/docusaurus-plugin-content-docs/current/components/micro-master-manifest-config.md +15 -0
- package/en/docusaurus-plugin-content-docs/current/components/micro-runtime-config.md +18 -0
- package/en/docusaurus-plugin-content-docs/current/components/router-legacy-tip.md +1 -0
- package/en/docusaurus-plugin-content-docs/current/configure/app/auto-load-plugin.md +62 -0
- package/en/docusaurus-plugin-content-docs/current/configure/app/deploy/microFrontend.md +54 -0
- package/en/docusaurus-plugin-content-docs/current/configure/app/output/ssg.md +226 -0
- package/en/docusaurus-plugin-content-docs/current/configure/app/runtime/master-app.md +20 -39
- package/en/docusaurus-plugin-content-docs/current/configure/app/runtime/router.md +17 -4
- package/en/docusaurus-plugin-content-docs/current/configure/app/runtime/state.md +17 -4
- package/en/docusaurus-plugin-content-docs/current/configure/app/server/enable-framework-ext.md +47 -0
- package/en/docusaurus-plugin-content-docs/current/guides/advanced-features/bff/frameworks.md +2 -0
- package/en/docusaurus-plugin-content-docs/current/guides/advanced-features/bff/function.md +10 -6
- package/en/docusaurus-plugin-content-docs/current/guides/advanced-features/ssg.md +6 -2
- package/en/docusaurus-plugin-content-docs/current/guides/basic-features/css/_category_.json +4 -0
- package/en/docusaurus-plugin-content-docs/current/guides/basic-features/data-fetch.md +1 -1
- package/en/docusaurus-plugin-content-docs/current/guides/basic-features/routes.md +0 -2
- package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/_category_.json +5 -0
- package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/framework-plugin/extend.md +162 -0
- package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/framework-plugin/hook-list.md +803 -0
- package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/framework-plugin/hook.md +169 -0
- package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/framework-plugin/implement.md +247 -0
- package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/framework-plugin/introduction.md +49 -0
- package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/framework-plugin/plugin-api.md +116 -0
- package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/framework-plugin/relationship.md +118 -0
- package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/generator/config/common.md +1 -1
- package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/generator/config/module.md +3 -1
- package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/generator/config/mwa.md +1 -9
- package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/generator/project.md +2 -2
- package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/micro-frontend/_category_.json +4 -0
- package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/micro-frontend/c01-introduction.md +29 -0
- package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/micro-frontend/c02-development.md +191 -0
- package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/micro-frontend/c03-main-app.md +246 -0
- package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/micro-frontend/c04-communicate.md +54 -0
- package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/micro-frontend/c05-mixed-stack.md +24 -0
- package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/model/_category_.json +4 -0
- package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/model/auto-actions.md +90 -0
- package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/model/computed-state.md +151 -0
- package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/model/define-model.md +66 -0
- package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/model/faq.md +43 -0
- package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/model/manage-effects.md +259 -0
- package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/model/model-communicate.md +219 -0
- package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/model/performance.md +173 -0
- package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/model/quick-start.md +116 -0
- package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/model/redux-integration.md +21 -0
- package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/model/test-model.md +43 -0
- package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/model/typescript-best-practice.md +71 -0
- package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/model/use-model.md +244 -0
- package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/model/use-out-of-modernjs.md +51 -0
- package/en/docusaurus-plugin-content-docs/current/tutorials/first-app/_category_.json +5 -0
- package/en/docusaurus-plugin-content-docs/current/tutorials/first-app/c01-start.md +99 -0
- package/en/docusaurus-plugin-content-docs/current/tutorials/first-app/c02-component.md +56 -0
- package/en/docusaurus-plugin-content-docs/current/tutorials/first-app/c03-css.md +324 -0
- package/en/docusaurus-plugin-content-docs/current/tutorials/first-app/c04-routes.md +169 -0
- package/en/docusaurus-plugin-content-docs/current/tutorials/first-app/c05-loader.md +82 -0
- package/en/docusaurus-plugin-content-docs/current/tutorials/first-app/c06-model.md +260 -0
- package/en/docusaurus-plugin-content-docs/current/tutorials/first-app/c07-container.md +283 -0
- package/en/docusaurus-plugin-content-docs/current/tutorials/first-app/c08-entries.md +137 -0
- package/en/docusaurus-plugin-content-docs/current/tutorials/foundations/_category_.json +1 -1
- package/en/docusaurus-plugin-content-docs/current/tutorials/foundations/introduction.md +5 -3
- package/package.json +4 -4
- package/zh/apis/app/runtime/core/use-module-apps.md +2 -0
- package/zh/apis/app/runtime/router/router.md +169 -371
- package/zh/components/enable-bff.md +36 -0
- package/zh/components/micro-master-manifest-config.md +15 -0
- package/zh/components/router-legacy-tip.md +1 -0
- package/zh/configure/app/auto-load-plugin.md +62 -0
- package/zh/configure/app/deploy/microFrontend.md +0 -10
- package/zh/configure/app/output/ssg.md +1 -5
- package/zh/configure/app/runtime/master-app.md +4 -18
- package/zh/configure/app/runtime/router.md +19 -4
- package/zh/configure/app/runtime/state.md +7 -7
- package/zh/configure/app/server/enable-framework-ext.md +47 -0
- package/zh/configure/app/server/port.md +1 -1
- package/zh/configure/app/tools/_category_.json +1 -1
- package/zh/guides/advanced-features/bff/frameworks.md +2 -0
- package/zh/guides/advanced-features/bff/function.md +7 -5
- package/zh/guides/advanced-features/eslint.md +2 -1
- package/zh/guides/advanced-features/ssg.md +4 -0
- package/zh/guides/basic-features/data-fetch.md +1 -1
- package/zh/guides/basic-features/env-vars.md +1 -1
- package/zh/guides/basic-features/routes.md +0 -3
- package/zh/guides/topic-detail/generator/config/module.md +3 -1
- package/zh/guides/topic-detail/generator/config/mwa.md +1 -9
- package/zh/guides/topic-detail/model/quick-start.md +1 -1
- package/zh/tutorials/first-app/c06-model.md +5 -1
- package/zh/tutorials/first-app/c08-entries.md +1 -1
- package/zh/tutorials/foundations/introduction.md +5 -3
- package/en/docusaurus-plugin-content-docs/current/apis/app/overview.md +0 -12
- package/en/docusaurus-plugin-content-docs/current/configure/app/bff/fetcher.md +0 -28
- package/en/docusaurus-plugin-content-docs/current/configure/app/dev/with-master-app.md +0 -31
- package/en/docusaurus-plugin-content-docs/current/guides/overview.md +0 -11
- package/en/docusaurus-plugin-content-docs/current/tutorials/foundations/basic.md +0 -8
- package/zh/apis/app/overview.md +0 -11
- package/zh/apis/monorepo/overview.md +0 -11
- package/zh/configure/app/bff/fetcher.md +0 -31
- package/zh/configure/app/dev/with-master-app.md +0 -32
- package/zh/guides/overview.md +0 -11
- package/zh/tutorials/foundations/basic.md +0 -8
@@ -0,0 +1,90 @@
|
|
1
|
+
---
|
2
|
+
sidebar_position: 6
|
3
|
+
title: 自动生成 Actions
|
4
|
+
---
|
5
|
+
|
6
|
+
在 [快速上手](/docs/guides/topic-detail/model/quick-start) 中,我们实现最简单的计数器 Model 也需要 10 行代码。
|
7
|
+
实际上,Modern.js 支持根据声明的 `state` 类型,自动生成常用的 Actions,从而简化模板代码量。当前支持的类型有:
|
8
|
+
|
9
|
+
- 原始数据类型
|
10
|
+
- 数组类型
|
11
|
+
- 简单对象类型(Plain Object)
|
12
|
+
|
13
|
+
## 原始数据类型
|
14
|
+
|
15
|
+
```ts
|
16
|
+
const countModel = model('count').define({ state: 1 });
|
17
|
+
```
|
18
|
+
|
19
|
+
如上我们仅用一行就完成了一个简单的 `countModel`。使用 Model 的示例代码如下:
|
20
|
+
|
21
|
+
```tsx
|
22
|
+
function Counter() {
|
23
|
+
const [state, actions] = useModel(countModel);
|
24
|
+
|
25
|
+
useEffect(() => {
|
26
|
+
// 增加 1
|
27
|
+
actions.setState(state + 1);
|
28
|
+
}, []);
|
29
|
+
}
|
30
|
+
```
|
31
|
+
|
32
|
+
## 数组类型
|
33
|
+
|
34
|
+
当 State 为数组类型时,自动生成 Actions 的示例代码如下:
|
35
|
+
|
36
|
+
```ts
|
37
|
+
const countModel = model('count').define({ state: [] });
|
38
|
+
|
39
|
+
function Counter() {
|
40
|
+
const [state, actions] = useModel(countModel);
|
41
|
+
|
42
|
+
useEffect(() => {
|
43
|
+
actions.push(1);
|
44
|
+
actions.pop();
|
45
|
+
actions.shift();
|
46
|
+
actions.unshift(1);
|
47
|
+
actions.concat([1]);
|
48
|
+
actions.splice(0, 1, 2);
|
49
|
+
actions.filter(value => value > 1);
|
50
|
+
}, []);
|
51
|
+
}
|
52
|
+
```
|
53
|
+
|
54
|
+
我们可以使用 JavaScript Array 对象的方法,修改 State。
|
55
|
+
|
56
|
+
## 简单对象类型
|
57
|
+
|
58
|
+
当 State 为简单对象类型时,自动生成 Actions 的示例代码如下:
|
59
|
+
|
60
|
+
```ts
|
61
|
+
const countModel = model('count').define({
|
62
|
+
state: {
|
63
|
+
a: 1,
|
64
|
+
b: [],
|
65
|
+
c: {},
|
66
|
+
},
|
67
|
+
});
|
68
|
+
|
69
|
+
function Counter() {
|
70
|
+
const [state, actions] = useModel(countModel);
|
71
|
+
|
72
|
+
useEffect(() => {
|
73
|
+
actions.setA(2);
|
74
|
+
actions.setB([1]);
|
75
|
+
actions.setC({ a: 1 });
|
76
|
+
}, []);
|
77
|
+
}
|
78
|
+
```
|
79
|
+
|
80
|
+
根据 `a`、`b`、`c` 三个不同的字段分别生成 `setA`、`setB`、`setC` 三个 Actions。
|
81
|
+
|
82
|
+
:::info 注
|
83
|
+
当用户自定义的 Action 和 Modern.js 自动生成的 Action 名字一致时,用户自定义的 Action 优先级更高。例如,
|
84
|
+
在 `countModel` 中已经自定义 `setA` 这个 Action,调用 `actions.setA()` 时,最终执行的是用户自定义的 `setA`。
|
85
|
+
:::
|
86
|
+
|
87
|
+
|
88
|
+
:::info 补充信息
|
89
|
+
相关 API 的更多介绍,请参考[这里](/docs/apis/app/runtime/model/auto-actions)。
|
90
|
+
:::
|
@@ -0,0 +1,151 @@
|
|
1
|
+
---
|
2
|
+
sidebar_position: 4
|
3
|
+
title: 衍生状态
|
4
|
+
---
|
5
|
+
|
6
|
+
一些场景中,组件需要对 Model 中的 State 进行进一步计算,才能在组件中使用,这部分逻辑可以直接写在组件内部,也可以通过 Model 的衍生状态实现。
|
7
|
+
衍生状态定义在 Model 中的 `computed` 字段下。根据依赖的 Model 的不同、返回类型的不同,衍生状态的定义方法可以分为以下 3 种。
|
8
|
+
|
9
|
+
|
10
|
+
## 只依赖自身的 State
|
11
|
+
|
12
|
+
衍生状态只依赖当前 Model 的 State,State 会作为第一个参数,传入衍生状态的定义函数中。
|
13
|
+
|
14
|
+
例如, todo 应用的 State 有 `items` 和 `filter`,`filter` 用于过滤当前页面显示的 todo 项,所以我们定义了一个 `visibleTodos` 的衍生状态可以直接在组件中使用。示例代码如下:
|
15
|
+
|
16
|
+
```ts
|
17
|
+
/**
|
18
|
+
* 假设 todo item 结构如下:
|
19
|
+
{
|
20
|
+
id: string; // id
|
21
|
+
text: string; // todo 事项
|
22
|
+
completed: boolean; // 完成状态:0 代表未完成,1 代表完成
|
23
|
+
}
|
24
|
+
**/
|
25
|
+
|
26
|
+
const todoModel = model('todo').define({
|
27
|
+
state: {
|
28
|
+
items: [],
|
29
|
+
filter: 'ALL', // ALL: 显示全部;COMPLETED:显示完成的事项;ACTIVE:显示未完成的事项
|
30
|
+
},
|
31
|
+
computed: {
|
32
|
+
visibleTodos: state => {
|
33
|
+
switch (state.filter) {
|
34
|
+
case 'ALL':
|
35
|
+
return state.items;
|
36
|
+
case 'COMPLETED':
|
37
|
+
return todos.filter(t => t.completed);
|
38
|
+
case 'ACTIVE':
|
39
|
+
return todos.filter(t => !t.completed);
|
40
|
+
default:
|
41
|
+
return [];
|
42
|
+
}
|
43
|
+
},
|
44
|
+
},
|
45
|
+
});
|
46
|
+
```
|
47
|
+
|
48
|
+
|
49
|
+
衍生状态最终会和 Model 的 State 进行合并,因此,可以通过 Model 的 State 对象访问到衍生状态,例如,`visibleTodos` 在组件内的使用方式如下:
|
50
|
+
|
51
|
+
```ts
|
52
|
+
function Todo() {
|
53
|
+
const [state, actions] = useModel(todoModel);
|
54
|
+
|
55
|
+
return (
|
56
|
+
<div>
|
57
|
+
<div>
|
58
|
+
{state.visibleTodos.map(item => (
|
59
|
+
<div key={item.id}>{item.text}</div>
|
60
|
+
))}
|
61
|
+
</div>
|
62
|
+
</div>
|
63
|
+
);
|
64
|
+
}
|
65
|
+
```
|
66
|
+
|
67
|
+
## 依赖其他 Model 的 State
|
68
|
+
|
69
|
+
除了依赖当前 Model 的 State,衍生状态还依赖其他 Model 的 State,这时候衍生状态的定义格式为:
|
70
|
+
|
71
|
+
```ts
|
72
|
+
[stateKey]: [...depModels, (selfState, ...depModels) => computedState]
|
73
|
+
```
|
74
|
+
|
75
|
+
下面的示例,演示了 `barModel` 的衍生状态 `combinedValue` 是如何依赖 `fooModel` 的 State 的。
|
76
|
+
|
77
|
+
```ts
|
78
|
+
const fooModel = model('foo').define({
|
79
|
+
state: {
|
80
|
+
value: 'foo',
|
81
|
+
},
|
82
|
+
});
|
83
|
+
|
84
|
+
const barModel = model('bar').define({
|
85
|
+
state: {
|
86
|
+
value: 'foo',
|
87
|
+
},
|
88
|
+
computed: {
|
89
|
+
combinedValue: [
|
90
|
+
fooModel,
|
91
|
+
(state, fooState) => state.value + fooState.value,
|
92
|
+
],
|
93
|
+
},
|
94
|
+
});
|
95
|
+
```
|
96
|
+
|
97
|
+
## 函数类型的衍生状态
|
98
|
+
|
99
|
+
衍生状态还可以返回一个函数。这时候衍生状态的定义格式为:
|
100
|
+
|
101
|
+
```ts
|
102
|
+
[stateKey]: (state) => (...args) => computedState // 只依赖自身的 state
|
103
|
+
[stateKey]: [...depModels, (selfState, ...depModels) => (...args) => computedState] // 依赖其他 Model 的 state
|
104
|
+
```
|
105
|
+
|
106
|
+
假设,todo 应用的 state 不存储 `filter` 状态,而是直接在组件中使用,那么 `visibleTodos` 可以是一个函数类型的值,这个函数在组件中使用的时候,接收 `filter` 参数。如下所示:
|
107
|
+
|
108
|
+
```ts
|
109
|
+
const todoModel = model('todo').define({
|
110
|
+
state: {
|
111
|
+
items: [],
|
112
|
+
},
|
113
|
+
computed: {
|
114
|
+
visibleTodos: state => filter => {
|
115
|
+
switch (filter) {
|
116
|
+
case 'ALL':
|
117
|
+
return state.items;
|
118
|
+
case 'COMPLETED':
|
119
|
+
return todos.filter(t => t.completed);
|
120
|
+
case 'ACTIVE':
|
121
|
+
return todos.filter(t => !t.completed);
|
122
|
+
default:
|
123
|
+
return [];
|
124
|
+
}
|
125
|
+
},
|
126
|
+
},
|
127
|
+
});
|
128
|
+
|
129
|
+
function Todo(props) {
|
130
|
+
// filter 状态通过 props 传入
|
131
|
+
const { filter } = props;
|
132
|
+
const [state, actions] = useModel(todoModel);
|
133
|
+
|
134
|
+
// 计算得到最终要显示的 todos
|
135
|
+
const todos = state.visibleTodos(filter);
|
136
|
+
|
137
|
+
return (
|
138
|
+
<div>
|
139
|
+
<div>
|
140
|
+
{todos.map(item => (
|
141
|
+
<div key={item.id}>{item.text}</div>
|
142
|
+
))}
|
143
|
+
</div>
|
144
|
+
</div>
|
145
|
+
);
|
146
|
+
}
|
147
|
+
```
|
148
|
+
|
149
|
+
:::info 更多参考
|
150
|
+
[使用 Model](/docs/guides/topic-detail/model/computed-state)
|
151
|
+
:::
|
@@ -0,0 +1,66 @@
|
|
1
|
+
---
|
2
|
+
sidebar_position: 2
|
3
|
+
title: 创建 Model
|
4
|
+
---
|
5
|
+
|
6
|
+
上一节的计数器应用中,我们简单演示了如何创建一个 Model。本节我们将详细介绍 Model 的创建方法。
|
7
|
+
|
8
|
+
通过 `model` API 创建 Model,例如,`model('foo')` 表示要创建一个名为 `foo` 的 Model,通过调用 `model('foo')` 返回的 `define` 函数,定义 Model 包含的 State、Actions 等:
|
9
|
+
|
10
|
+
``` js
|
11
|
+
import { model, useModel } from '@modern-js/runtime/model';
|
12
|
+
|
13
|
+
const fooModel = model('foo').define({
|
14
|
+
state: {
|
15
|
+
value: 'foo',
|
16
|
+
},
|
17
|
+
actions: {
|
18
|
+
setValue: (state, payload){
|
19
|
+
state.value = payload
|
20
|
+
}
|
21
|
+
}
|
22
|
+
});
|
23
|
+
```
|
24
|
+
|
25
|
+
:::info 注
|
26
|
+
- Model 中的 Action 中不能包含有副作用的逻辑,如请求 HTTP 接口,访问 localStorage 等。
|
27
|
+
- `setValue` 内部直接修改了入参 State,看起来是违反了纯函数的定义,实际上 Reduck 内部使用 [immer](https://github.com/immerjs/immer) 来修改不可变对象,保证了这种写法不会影响对象的不可变性,所以 `setValue` 仍然是一个纯函数。当然,你也可以直接在 Action 中返回一个新对象,不过这样的写法会更加复杂一些。
|
28
|
+
:::
|
29
|
+
|
30
|
+
`define` 接收的参数,只是对 Model 原始结构的描述:内部定义的 State、Actions 等。`define` 返回值 `fooModel` 才是真正创建得到的 Model 对象。例如,虽然 `setValue`,有 2 个参数,但是当真正调用 `setValue` 这个 Action 时,我们只需要传入 `payload` 一个参数,因为我们调用的是 `fooModel` 上的 Action 方法,而不是 Model 原始结构上描述的 Action 方法。详细参考[使用 Model](/docs/guides/topic-detail/model/use-model)。
|
31
|
+
|
32
|
+
`define` 除了接收对象类型的参数,还可以接收函数类型的参数。例如:
|
33
|
+
|
34
|
+
``` js
|
35
|
+
import { model, useModel } from '@modern-js/runtime/model';
|
36
|
+
|
37
|
+
const fooModel = model('foo').define((context, utils) => {
|
38
|
+
return {
|
39
|
+
state: {
|
40
|
+
value: 'foo',
|
41
|
+
},
|
42
|
+
actions: {
|
43
|
+
setValue: (state, payload){
|
44
|
+
state.value = payload
|
45
|
+
}
|
46
|
+
}
|
47
|
+
}
|
48
|
+
});
|
49
|
+
```
|
50
|
+
|
51
|
+
通过函数定义 Model 时,函数内部会自动传入 `context`、`utils` 2 个参数,`context` 是 Reduck 的上下文对象,可以获取到 `store` 对象,`utils` 提供了一组工具函数,方便实现 Model 通信等复杂功能需求。
|
52
|
+
|
53
|
+
|
54
|
+
Model 支持复制。例如:
|
55
|
+
|
56
|
+
``` ts
|
57
|
+
const barModel = fooModel('bar');
|
58
|
+
```
|
59
|
+
|
60
|
+
barModel 是基于 fooModel 创建出一个的新的 Model 对象,类比面向对象编程语言中的概念,barModel 和 fooModel 是基于同一个类(Class)创建出的 2 个实例对象。当两个模块的状态管理逻辑相同,例如一个页面中的两个 tab 模块,使用的数据的结构和逻辑相同,区别只是从不同的接口获取数据,那么可以通过 Model 复制的方式,创建 2 个不同的 Model 对象。
|
61
|
+
|
62
|
+
|
63
|
+
:::info 补充信息
|
64
|
+
本节涉及的 API 的详细定义,请参考[这里](/docs/apis/app/runtime/model/model_)。
|
65
|
+
:::
|
66
|
+
|
@@ -0,0 +1,43 @@
|
|
1
|
+
---
|
2
|
+
sidebar_position: 13
|
3
|
+
title: 常见问题
|
4
|
+
---
|
5
|
+
|
6
|
+
# 常见问题
|
7
|
+
|
8
|
+
## 浏览器兼容性
|
9
|
+
|
10
|
+
Reduck 的编译构建产物默认使用 ES6 语法,如果你需要支持更低版本的浏览器,请将 `@modern-js-reduck` 命名空间下的所有包加入到应用的编译过程。
|
11
|
+
|
12
|
+
:::info 补充信息
|
13
|
+
Reduck 使用的 [`@babel/preset-env`](https://babeljs.io/docs/en/babel-preset-env) 的详细[配置](https://github.com/modern-js-dev/reduck/blob/main/common/config.js#L10~L17)。
|
14
|
+
:::
|
15
|
+
|
16
|
+
## 微前端子应用 Model 访问主应用 Model
|
17
|
+
|
18
|
+
微前端子应用 Model 访问主应用 Model 时,如果该 **Model** 在主应用尚未挂载,会自动挂载到子应用上。
|
19
|
+
|
20
|
+
示例:
|
21
|
+
|
22
|
+
```ts
|
23
|
+
import { useModel } from '@modern-js/runtime/model';
|
24
|
+
import parentModel from '@MasterApp/models/todoModel';
|
25
|
+
|
26
|
+
function SubModelApp() {
|
27
|
+
const [state, actions] = useModel(parentModel);
|
28
|
+
|
29
|
+
return <div>...</div>
|
30
|
+
}
|
31
|
+
```
|
32
|
+
|
33
|
+

|
34
|
+
|
35
|
+
为了避免意外降级挂载,建议将主应用所需要共享的 Model 预先挂载:
|
36
|
+
|
37
|
+
```ts
|
38
|
+
// App 是主应用的入口组件,sharedModel1、sharedModel2 是需要共享的 Model。
|
39
|
+
App.models = [
|
40
|
+
sharedModel1,
|
41
|
+
sharedModel2
|
42
|
+
]
|
43
|
+
```
|
@@ -0,0 +1,259 @@
|
|
1
|
+
---
|
2
|
+
sidebar_position: 5
|
3
|
+
title: 副作用管理
|
4
|
+
---
|
5
|
+
|
6
|
+
Model 中的 Action 必须是一个纯函数,执行过程中不会产生任何副作用。但在真实的业务中,我们会遇到很多副作用场景,如:请求 HTTP 接口获取状态数据,或者在更新状态的同时修改 localStorage、发送事件等。在 Reduck 中,是通过 Model 的 Effects 函数管理副作用的。
|
7
|
+
|
8
|
+
## 副作用对 State 修改
|
9
|
+
|
10
|
+
副作用修改 State,最常见的场景就是请求 HTTP 接口,更新状态数据。
|
11
|
+
|
12
|
+
我们以一个简单的 `todoModel` 为例。其有一个 `load` 的副作用函数,请求远端的 TODO 列表,请求成功之后更新 `state.items` 字段。
|
13
|
+
|
14
|
+
```ts
|
15
|
+
const todoModel = model('todo').define({
|
16
|
+
state: {
|
17
|
+
items: [],
|
18
|
+
loading: false,
|
19
|
+
error: null,
|
20
|
+
},
|
21
|
+
actions: {
|
22
|
+
load: {
|
23
|
+
pending(state) {
|
24
|
+
state.loading = true;
|
25
|
+
},
|
26
|
+
fulfilled(state, items) {
|
27
|
+
state.items = items;
|
28
|
+
state.loading = false;
|
29
|
+
},
|
30
|
+
rejected(state, error) {
|
31
|
+
state.error = error;
|
32
|
+
state.loading = false;
|
33
|
+
},
|
34
|
+
},
|
35
|
+
},
|
36
|
+
effects: {
|
37
|
+
// Promise 副作用
|
38
|
+
async load() {
|
39
|
+
return new Promise(resolve => {
|
40
|
+
setTimeout(() => resolve(['Learn Modern.js']), 2000);
|
41
|
+
});
|
42
|
+
},
|
43
|
+
},
|
44
|
+
});
|
45
|
+
```
|
46
|
+
|
47
|
+
副作用函数统一定义在 `effects` 字段下。这里我们写了一个 `load` 函数,它返回一个 Promise,Promise 执行成功后,返回 TODO 列表 `["Lerna Modern.js"]`。
|
48
|
+
|
49
|
+
副作用函数需要和 actions 配合使用,才能完成状态的修改。因此,我们在 `actions` 中定义了一个 `load`(命名需要和 `effects` 下的副作用函数的名字保持一致)对象,包含 `pending`、`fulfilled`、`rejected` 3 个 action,分别是对副作用函数 `load` 返回的 `Promise` 的三种状态( pending、fulfilled、rejected )的处理:
|
50
|
+
|
51
|
+
- `pending`:接收当前状态 `state` 作为参数,新的状态中 `loading` 设为 `true`。
|
52
|
+
- `fulfilled`:接收当前状态 `state` 和 Promise fulfilled 状态的值 `items` 为参数,新的状态中 `items` 等于参数 `items`、`loading` 设为 `false`。
|
53
|
+
- `rejected`:接收当前状态 `state` 和 Promise rejected 状态的错误 `error` 为参数,新的状态中 `error` 等于参数 `error`、`loading` 设为 `false`。
|
54
|
+
|
55
|
+
组件中如何调用 effects 函数呢? effects 函数会被合并到 actions 对象上,因此可以通过 actions 对象调用 effects 函数,如下所示:
|
56
|
+
|
57
|
+
```ts
|
58
|
+
function Todo() {
|
59
|
+
const [state, actions] = useModel(todoModel);
|
60
|
+
|
61
|
+
useEffect(() => {
|
62
|
+
// 调用 effects 函数
|
63
|
+
actions.load();
|
64
|
+
}, []);
|
65
|
+
|
66
|
+
if (state.loading) {
|
67
|
+
return <div>loading....</div>;
|
68
|
+
}
|
69
|
+
|
70
|
+
return (
|
71
|
+
<div>
|
72
|
+
<div>
|
73
|
+
{state.items.map((item, index) => (
|
74
|
+
<div key={index}>{item}</div>
|
75
|
+
))}
|
76
|
+
</div>
|
77
|
+
</div>
|
78
|
+
);
|
79
|
+
}
|
80
|
+
```
|
81
|
+
|
82
|
+
上面的示例中, `pending`、`fulfilled`、`rejected` 3 个 action,对于用于获取数据的 HTTP 请求场景下,一般都是需要的。Reduck 提供了一个工具函数 `handleEffect`,用于简化这种场景下的 action 创建。
|
83
|
+
|
84
|
+
`handleEffect` 约定这种副作用场景下, Model 的 State 结构包含 `result`、`error`、`pending` 3 个字段,初始值为:
|
85
|
+
|
86
|
+
```ts
|
87
|
+
{
|
88
|
+
result: null,
|
89
|
+
error: null,
|
90
|
+
pending: false,
|
91
|
+
}
|
92
|
+
```
|
93
|
+
|
94
|
+
调用 `handleEffect` 会返回如下数据结构:
|
95
|
+
|
96
|
+
``` ts
|
97
|
+
{
|
98
|
+
pending() { // ... },
|
99
|
+
fulfilled() { // ... },
|
100
|
+
rejected() { // ... }
|
101
|
+
}
|
102
|
+
```
|
103
|
+
|
104
|
+
这个数据结构和我们在 `actions` 下的 `load` 对象的数据结构是相同的。`handleEffect` 返回的对象,其实就是对应了 Effects 函数需要的 3 个 action。
|
105
|
+
|
106
|
+
|
107
|
+
利用 `handleEffect`,改写 `todoModel`:
|
108
|
+
|
109
|
+
```ts
|
110
|
+
const todoModel = model('todo').define({
|
111
|
+
state: {
|
112
|
+
items: [],
|
113
|
+
loading: false,
|
114
|
+
error: null,
|
115
|
+
},
|
116
|
+
actions: {
|
117
|
+
load: handleEffect({ result: 'items' }),
|
118
|
+
},
|
119
|
+
effects: {
|
120
|
+
// Promise 副作用
|
121
|
+
async load() {
|
122
|
+
return new Promise(resolve => {
|
123
|
+
setTimeout(() => resolve(['Learn Modern.js']), 2000);
|
124
|
+
});
|
125
|
+
},
|
126
|
+
},
|
127
|
+
});
|
128
|
+
```
|
129
|
+
|
130
|
+
`handleEffect` 接收的参数对象,将 `result` 设置为 `item`。因为 `todoModel` 的 state,使用 `items` 作为 key 保存 todo 列表数据,而不是使用 `handleEffect` 默认的 `result` 作为 key,所以这里需要进行配置。
|
131
|
+
|
132
|
+
明显可见,通过 `handleEffect` 实现的 `todoModel` 比之前的实现有了很大精简。
|
133
|
+
|
134
|
+
如果不希望 pending、fulfilled、rejected 3 种状态都被 `handleEffect` 自动处理,例如 fulfilled 需要手动处理较复杂的数据类型,但是 pending、rejected 依旧想进行自动化处理,可以参考如下写法:
|
135
|
+
|
136
|
+
```ts
|
137
|
+
actions: {
|
138
|
+
load: {
|
139
|
+
...handleEffect(),
|
140
|
+
fulfilled(state, payload) {
|
141
|
+
// 手动处理
|
142
|
+
},
|
143
|
+
},
|
144
|
+
},
|
145
|
+
```
|
146
|
+
|
147
|
+
:::info 补充信息
|
148
|
+
`handleEffect` [API](/docs/apis/app/runtime/model/handle-effect)。
|
149
|
+
:::
|
150
|
+
|
151
|
+
|
152
|
+
Effects 函数中,也支持手动调用 Actions,例如:
|
153
|
+
|
154
|
+
```ts
|
155
|
+
const todoModel = model('todo').define((context, utils) => ({
|
156
|
+
state: {
|
157
|
+
items: [],
|
158
|
+
loading: false,
|
159
|
+
error: null,
|
160
|
+
},
|
161
|
+
actions: {
|
162
|
+
pending(state) {
|
163
|
+
state.loading = true;
|
164
|
+
},
|
165
|
+
fulfilled(state, items) {
|
166
|
+
state.items = items;
|
167
|
+
state.loading = false;
|
168
|
+
},
|
169
|
+
},
|
170
|
+
effects: {
|
171
|
+
async load() {
|
172
|
+
// 通过 utils.use 获取当前 Model 对象的 actions
|
173
|
+
const [, actions] = utils.use(todoModel);
|
174
|
+
// 手动调用 action
|
175
|
+
actions.pending();
|
176
|
+
|
177
|
+
return new Promise(resolve => {
|
178
|
+
setTimeout(() => {
|
179
|
+
const items = ['Learn Modern.js'];
|
180
|
+
// 手动调用 action
|
181
|
+
actions.fulfilled(items);
|
182
|
+
resolve(items);
|
183
|
+
}, 2000);
|
184
|
+
});
|
185
|
+
},
|
186
|
+
},
|
187
|
+
}));
|
188
|
+
```
|
189
|
+
|
190
|
+
:::info 注
|
191
|
+
可以使用 `use` 函数加载其它 Model(包括 Model 自身),实现 [Model 间通信](/docs/guides/topic-detail/model/model-communicate)。
|
192
|
+
:::
|
193
|
+
|
194
|
+
|
195
|
+
## 副作用不影响 state
|
196
|
+
|
197
|
+
有些场景下,我们只需要读取 State,执行相关副作用逻辑,副作用不会修改 State。
|
198
|
+
|
199
|
+
例如,存储某些 State 到 `localStorage`:
|
200
|
+
|
201
|
+
```ts
|
202
|
+
const fooModel = model('foo').define((context, utils) => ({
|
203
|
+
state: {
|
204
|
+
value: 'foo',
|
205
|
+
},
|
206
|
+
effects: {
|
207
|
+
setLocalStorage(key) {
|
208
|
+
const [state] = utils.use(fooModel);
|
209
|
+
localStorage.set(key, state.value);
|
210
|
+
return 'success';
|
211
|
+
},
|
212
|
+
},
|
213
|
+
}));
|
214
|
+
```
|
215
|
+
|
216
|
+
或者是向服务端发送数据:
|
217
|
+
|
218
|
+
```ts
|
219
|
+
const fooModel = model('foo').define({
|
220
|
+
state: {
|
221
|
+
value: 'foo',
|
222
|
+
},
|
223
|
+
effects: {
|
224
|
+
async sendData(data) {
|
225
|
+
const res = await fetch('url', {
|
226
|
+
method: 'POST',
|
227
|
+
body: data,
|
228
|
+
});
|
229
|
+
return res.json();
|
230
|
+
},
|
231
|
+
},
|
232
|
+
});
|
233
|
+
```
|
234
|
+
|
235
|
+
## 副作用函数返回值
|
236
|
+
|
237
|
+
有时候,我们希望能根据副作用函数的执行结果,直接执行后续逻辑。这时候,就需要使用 Effects 函数的返回。
|
238
|
+
|
239
|
+
例如,当点击发送按钮,发送数据成功后,立即关闭当前的弹窗;如果失败,显示错误信息。我们可以通过如下代码实现:
|
240
|
+
|
241
|
+
```ts
|
242
|
+
// 代码仅做示意,不能执行
|
243
|
+
// 组件内部 发送按钮 的响应函数
|
244
|
+
const handleClick = async () => {
|
245
|
+
// sendData 返回代表状态的字符串
|
246
|
+
const result = await actions.sendData('some data');
|
247
|
+
if (result === 'success') {
|
248
|
+
// 关闭弹窗
|
249
|
+
closeModal();
|
250
|
+
} else {
|
251
|
+
// 显示错误
|
252
|
+
showError(result);
|
253
|
+
}
|
254
|
+
};
|
255
|
+
```
|
256
|
+
|
257
|
+
:::info 补充信息
|
258
|
+
- [示例代码](https://github.com/modern-js-dev/modern-js-examples/tree/main/series/tutorials/runtime-api/model/effects)
|
259
|
+
:::
|