@modern-js/main-doc 2.7.0 → 2.8.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (73) hide show
  1. package/.turbo/turbo-build.log +1 -1
  2. package/CHANGELOG.md +17 -0
  3. package/README.md +2 -2
  4. package/en/apis/app/commands.mdx +2 -0
  5. package/en/apis/app/runtime/model/connect.mdx +1 -1
  6. package/en/apis/app/runtime/model/model_.mdx +1 -1
  7. package/en/apis/app/runtime/model/use-model.mdx +1 -1
  8. package/en/apis/app/runtime/web-server/hook.mdx +2 -2
  9. package/en/apis/app/runtime/web-server/middleware.mdx +33 -9
  10. package/en/components/enable-bff.mdx +4 -4
  11. package/en/components/init-rspack-app.mdx +7 -0
  12. package/en/configure/app/bff/enable-handle-web.mdx +24 -0
  13. package/en/configure/app/server/enable-framework-ext.mdx +1 -1
  14. package/en/guides/advanced-features/bff/_category_.json +1 -1
  15. package/en/guides/advanced-features/eslint.mdx +30 -32
  16. package/en/guides/advanced-features/low-level.mdx +1 -1
  17. package/en/guides/advanced-features/rspack-start.mdx +13 -17
  18. package/en/guides/advanced-features/web-server.mdx +87 -20
  19. package/en/guides/concept/builder.mdx +1 -1
  20. package/en/guides/topic-detail/framework-plugin/extend.mdx +20 -19
  21. package/en/guides/topic-detail/framework-plugin/hook-list.mdx +156 -155
  22. package/en/guides/topic-detail/framework-plugin/hook.mdx +58 -43
  23. package/en/guides/topic-detail/framework-plugin/implement.mdx +47 -49
  24. package/en/guides/topic-detail/framework-plugin/introduction.mdx +22 -23
  25. package/en/guides/topic-detail/framework-plugin/plugin-api.mdx +13 -13
  26. package/en/guides/topic-detail/framework-plugin/relationship.mdx +30 -30
  27. package/en/guides/topic-detail/generator/plugin/develop.mdx +1 -1
  28. package/en/guides/topic-detail/micro-frontend/c01-introduction.mdx +17 -17
  29. package/en/guides/topic-detail/micro-frontend/c02-development.mdx +76 -78
  30. package/en/guides/topic-detail/micro-frontend/c03-main-app.mdx +57 -51
  31. package/en/guides/topic-detail/micro-frontend/c04-communicate.mdx +11 -11
  32. package/en/guides/topic-detail/micro-frontend/c05-mixed-stack.mdx +13 -13
  33. package/en/guides/topic-detail/model/auto-actions.mdx +18 -21
  34. package/en/guides/topic-detail/model/computed-state.mdx +27 -25
  35. package/en/guides/topic-detail/model/define-model.mdx +14 -14
  36. package/en/guides/topic-detail/model/faq.mdx +12 -13
  37. package/en/guides/topic-detail/model/manage-effects.mdx +43 -52
  38. package/en/guides/topic-detail/model/model-communicate.mdx +47 -45
  39. package/en/guides/topic-detail/model/performance.mdx +22 -23
  40. package/en/guides/topic-detail/model/quick-start.mdx +29 -28
  41. package/en/guides/topic-detail/model/redux-integration.mdx +7 -7
  42. package/en/guides/topic-detail/model/test-model.mdx +11 -11
  43. package/en/guides/topic-detail/model/typescript-best-practice.mdx +16 -15
  44. package/en/guides/topic-detail/model/use-model.mdx +40 -45
  45. package/en/guides/topic-detail/model/use-out-of-modernjs.mdx +16 -16
  46. package/en/guides/troubleshooting/cli.mdx +2 -2
  47. package/package.json +5 -5
  48. package/zh/apis/app/commands.mdx +2 -0
  49. package/zh/apis/app/runtime/model/connect.mdx +1 -1
  50. package/zh/apis/app/runtime/model/model_.mdx +1 -1
  51. package/zh/apis/app/runtime/model/use-model.mdx +1 -1
  52. package/zh/apis/app/runtime/web-server/hook.mdx +2 -4
  53. package/zh/apis/app/runtime/web-server/middleware.mdx +30 -10
  54. package/zh/apis/monorepo/commands/gen-release-note.mdx +3 -3
  55. package/zh/components/enable-bff.mdx +4 -4
  56. package/zh/components/init-rspack-app.mdx +7 -0
  57. package/zh/components/release-note.mdx +1 -1
  58. package/zh/configure/app/bff/enable-handle-web.mdx +24 -0
  59. package/zh/configure/app/server/enable-framework-ext.mdx +1 -1
  60. package/zh/guides/advanced-features/bff/_category_.json +1 -1
  61. package/zh/guides/advanced-features/rspack-start.mdx +13 -17
  62. package/zh/guides/advanced-features/web-server.mdx +81 -16
  63. package/zh/guides/concept/builder.mdx +1 -1
  64. package/zh/guides/topic-detail/changesets/github.mdx +2 -2
  65. package/zh/guides/topic-detail/changesets/release-note.mdx +1 -1
  66. package/zh/guides/topic-detail/framework-plugin/plugin-api.mdx +2 -2
  67. package/zh/guides/topic-detail/generator/plugin/develop.mdx +1 -1
  68. package/zh/guides/topic-detail/model/faq.mdx +1 -1
  69. package/zh/guides/topic-detail/model/manage-effects.mdx +1 -1
  70. package/zh/guides/topic-detail/model/model-communicate.mdx +1 -1
  71. package/zh/guides/topic-detail/model/performance.mdx +1 -1
  72. package/zh/guides/topic-detail/model/quick-start.mdx +2 -2
  73. package/zh/guides/topic-detail/model/use-model.mdx +3 -3
@@ -1,45 +1,45 @@
1
1
  ---
2
2
  sidebar_position: 3
3
- title: 开发主应用
3
+ title: Develop Main App
4
4
  ---
5
- # 开发主应用
5
+ # Develop Main App
6
6
 
7
- 在上一章 [体验微前端](/guides/topic-detail/micro-frontend/c02-development) 通过一个示例演示了如何创建和配置微前端子应用,通过本章你可以进一步了解如何开发主应用,以及它的常见配置。
7
+ In the previous [Experience micro frontend](/guides/topic-detail/micro-frontend/c02-development), an example was used to demonstrate how to create and configure micro frontend sub-applications. Through this chapter, you can further understand how to develop the main application, and its common configuration.
8
8
 
9
- 在通过 `@modern-js/create` 命令创建应用工程后,可以在项目中执行 `pnpm run new`(实际执行了 `modern new` 命令),在选择了「微前端」模式后,会安装微前端依赖依赖,只需手动注册插件即可。
9
+ After creating an application project through the `@modern-js/create` command, you can execute `pnpm run new` in the project (the `modern new` command is actually executed). After selecting the 「micro frontend」 mode, the micro frontend will be installed. Dependencies, just register the plugin manually.
10
10
 
11
11
  import EnableMicroFrontend from "@site-docs/components/enable-micro-frontend";
12
12
 
13
13
  <EnableMicroFrontend />
14
14
 
15
- ## 注册子应用信息
15
+ ## Register Sub-app
16
16
 
17
- 当在 `masterApp` 配置上提供了信息后,将会认为该应用为主应用,目前存在两种子应用信息的配置方式,这两种方式分别应用于不同的场景。
17
+ When the information is provided on the `masterApp` configuration, the application will be considered the main application. At present, there are two configuration methods for sub-app information, and these two methods are applied to different scenarios.
18
18
 
19
- ### 直接注册子应用信息
19
+ ### Register sub-app info directly
20
20
 
21
- 可以直接通过配置注册子应用信息:
21
+ You can register the sub-application information directly through the configuration:
22
22
 
23
23
  :::tip
24
- 可以通过 API [defineConfig](/apis/app/runtime/app/define-config) 在运行时进行配置。
25
- 当参数为函数时无法被序列化到产物代码,所以在涉及到函数之类的配置时请通过 defineConfig 来进行配置
24
+ It can be configured at runtime via the API [defineConfig](/apis/app/runtime/app/define-config).
26
25
 
26
+ When the parameter is a function, it cannot be serialized to the product code, so please configure it through defineConfig when it comes to configuration such as functions
27
27
  :::
28
28
 
29
29
  import MicroRuntimeConfig from "@site-docs/components/micro-runtime-config";
30
30
 
31
31
  <MicroRuntimeConfig />
32
32
 
33
- ### 自定义远程应用列表
33
+ ### Custom remote app list
34
34
 
35
- 通过该函数可以拉取远程的子应用列表,并将其注册至运行时框架:
35
+ This function allows you to pull a list of remote child applications and register them with the runtime framework:
36
36
 
37
37
  ```ts title="App.tsx"
38
38
  defineConfig(App, {
39
39
  masterApp: {
40
40
  manifest: {
41
41
  getAppList: async () => {
42
- // 可以从其他远程接口获取
42
+ // get from remote api
43
43
  return [{
44
44
  name: 'Table',
45
45
  entry: 'http://localhost:8001',
@@ -55,24 +55,24 @@ defineConfig(App, {
55
55
  });
56
56
  ```
57
57
 
58
- ## 渲染子应用
58
+ ## Renderer sub-app
59
59
 
60
- 在微前端中分为两种加载子应用的方式:
60
+ There are two ways to load sub-app in micro frontend:
61
61
 
62
- 1. **子应用组件** 获取到每个子应用的组件,之后就可以像使用普通的 `React` 组件一样渲染微前端的子应用。
63
- 2. **集中式路由** 通过集中式的路由配置,自动根据当前页面 `pathname` 激活渲染对应的子应用。
62
+ 1. **Sub-app component ** Get the components of each sub-app, and then you can render the sub-app of micro frontend just like using ordinary'React 'components.
63
+ 2. **Centralized routing** Through centralized routing configuration, the corresponding sub-app of rendering is automatically activated according to the current page `pathname`.
64
64
 
65
- ### 子应用组件
65
+ ### Sub-app component
66
66
 
67
- 开发者使用 `useModuleApps` 方法可以获取到各个子应用的组件。
67
+ Developers can use the `useModuleApps` method to obtain the components of each child application.
68
68
 
69
- 再通过 `router` 组件的结合运用,开发者可以自控式的根据不同的路由渲染不同的子应用。
69
+ Through the combined use of the `router` component, developers can autonomously render different sub-applications according to different routes.
70
70
 
71
- 假设我们的子应用列表配置如下:
71
+ Suppose our subapp list is configured as follows:
72
72
 
73
73
  <EnableMicroFrontend />
74
74
 
75
- 编辑主应用 `App.tsx` 文件如下:
75
+ `App.tsx` as follow:
76
76
 
77
77
  ```js title="App.tsx"
78
78
  import { useModuleApps } from '@modern-js/plugin-garfish/runtime';
@@ -81,9 +81,9 @@ import { RouterProvider, Route, createBrowserRouter, createRoutesFromElements, B
81
81
 
82
82
  const AppLayout = () => (
83
83
  <>
84
- <div><Link to={'/table'}>加载约定式路由子应用</Link></div>
85
- <div><Link to={'/dashboard'}>加载自控式路由子应用</Link></div>
86
- <div><Link to={'/'}>卸载子应用</Link></div>
84
+ <div><Link to={'/table'}>load file-based sub-app</Link></div>
85
+ <div><Link to={'/dashboard'}>load self-controlled sub-app</Link></div>
86
+ <div><Link to={'/'}>unmount sub-app</Link></div>
87
87
  <Outlet />
88
88
  </>
89
89
  )
@@ -91,13 +91,13 @@ const AppLayout = () => (
91
91
  export default () => {
92
92
  const { apps, MApp } = useModuleApps();
93
93
 
94
- // 使用的不是 MApp 组件,需要使用 createBrowserRouter 来创建路由
94
+ // Instead of using the MApp component, you need to use createBrowserRouter to create the route
95
95
  const router = createBrowserRouter(
96
96
  createRoutesFromElements(
97
97
  <Route path="/" element={<AppLayout />}>
98
98
  {apps.map(app => {
99
99
  const { Component } = app;
100
- // 模糊匹配,path 需要写成类似 abc/* 的模式
100
+ // Fuzzy match, path needs to be written in a pattern similar to abc/*
101
101
  return (
102
102
  <Route
103
103
  key={app.name}
@@ -125,12 +125,12 @@ export default () => {
125
125
  );
126
126
 
127
127
  return (
128
- // 方法一:使用 MApp 自动根据配置的 activeWhen 参数加载子应用(本项目配置在 modern.config.ts)
128
+ // Use MApp to automatically load sub-applications according to the configured activeWhen parameters (this project is configured in modern.config.ts)
129
129
  // <BrowserRouter>
130
130
  // <MApp />
131
131
  // </BrowserRouter>
132
132
 
133
- // 方法二:手动写 Route 组件方式加载子应用,方便于需要鉴权等需要前置操作的场景
133
+ // Manually write the Route component to load the sub-application, which is convenient for scenarios that require pre-operation such as authentication
134
134
  <>
135
135
  <RouterProvider router={router} />
136
136
  </>
@@ -138,17 +138,17 @@ export default () => {
138
138
  };
139
139
  ```
140
140
 
141
- 这里通过 `Route` 组件自定义了 **Table** 的激活路由为 **/table**, **Dashboard** 的激活路由为 **/dashboard**。
141
+ Here, the activation route of **Table** is customized as **/table** through the `Route` component, and the activation route of **Dashboard** is **/dashboard**.
142
142
 
143
- ### 集中式路由
143
+ ### Centralized routing
144
144
 
145
- **集中式路由** 是将子应用的激活路由集中配置的方式。我们给子应用列表信息添加 `activeWhen` 字段来启用 **集中式路由**。
145
+ **Centralized Routing** is a way to centrally configure active routes for subapps. We enable **Centralized Routing** by adding an `activeWhen` field to the subapp list information.
146
146
 
147
147
  <MicroRuntimeConfig />
148
148
 
149
- 然后在主应用中使用 `useModuleApp` 方法获取 `MApp` 组件, 并在主应用渲染 `MApp`。
149
+ Use `useModuleApp` api in Main App, get `MApp` component and then render it
150
150
 
151
- ```tsx title=主应用:App.tsx
151
+ ```tsx title=Main App:App.tsx
152
152
  import { useModuleApp } from '@modern-js/plugin-garfish/runtime';
153
153
 
154
154
  function App() {
@@ -162,18 +162,22 @@ function App() {
162
162
  }
163
163
  ```
164
164
 
165
- 这样启动应用后,访问 `/table` 路由,会渲染 `Table` 子应用,访问 `/dashboard` 路由,会渲染 `Dashboard` 子应用。
165
+ After starting the application in this way, accessing the '/table' route will render the'Table 'sub-application, and accessing the'/dashboard 'route will render the'Dashboard' sub-application.
166
166
 
167
- ### 两种模式混用
168
167
 
169
- 当然 **子应用组件** 和 **集中式路由** 是可以混合使用的。
168
+ ### Mix Mode
170
169
 
171
- - 一部分子应用作为 **子应用组件** 激活,另外一部分作为 **集中式路由** 激活。
172
- - 一部分子应用既可以作为 **集中式路由** 激活,也可以作为 **子应用组件** 激活。
173
170
 
174
- ### 添加 loading
171
+ Of course, **sub-application components** and **centralized routing** can be mixed.
172
+
173
+
174
+ - One molecular application is activated as a **sub-application component**, and the other part is activated as a **centralized routing**.
175
+ - A molecular application can be activated either as a **centralized routing** or as a **sub-application component**.
176
+
177
+ ### Loading
178
+
179
+ By configuring the `loadable` configuration, loading components can be added for 「centralized routing」 and 「sub-applicati」, and errors, timeouts, flashes, etc. can be considered, so as to provide users with a better user experience. The design and implementation of this function refer to [react-loadable](https://github.com/jamiebuilds/react-loadable), and the basic functions are similar.
175
180
 
176
- 通过配置 `loadable` 配置,可以为「集中式路由」、「子应用」添加 loading 组件,并可以考虑错误、超时、闪烁等情况的出现,从而为用户提供更好的用户体验。该功能的设计与实现参考至 [react-loadable](https://github.com/jamiebuilds/react-loadable),基本功能较为相似。
177
181
 
178
182
  ```tsx
179
183
  function Loading() {
@@ -191,9 +195,9 @@ function App(){
191
195
  }
192
196
  ```
193
197
 
194
- #### 增加错误状态
198
+ #### Add Error Status
195
199
 
196
- 当微前端子应用加载失败或渲染失败时,`loading component` 将会接收 `error` 参数(若没有错误时 error null
200
+ When the micro-frontend sub-application fails to load or render, the `loading component` will receive the `error` parameter (if there is no error, the error is null).
197
201
 
198
202
  ```tsx
199
203
  function Loading({ error }) {
@@ -205,11 +209,13 @@ function Loading({ error }) {
205
209
  }
206
210
  ```
207
211
 
208
- #### 避免 loading 闪退
212
+ #### Avoid Loading Flash Back
213
+
214
+ Sometimes the display time of the loading component may be less than 200ms, and the loading component will flash back at this time. Many user studies have proved that the loading flash back situation will cause the user to perceive that the loading content takes longer than the actual time.
209
215
 
210
- 有时 loading 组件的显示时间可能小于 200ms,这个时候会出现 loading 组件闪退的情况。许多用户研究证明,loading 闪退的情况会导致用户感知加载内容的耗时比实际耗时更长,在 loading 小于 200ms 时,不展示内容,用户会认为它更快。
216
+ When loading is less than 200ms, if the content is not displayed, the user will think it is faster.
211
217
 
212
- 所以 loading 组件还提供了 `pastDelay` 参数,超过设置的延迟展示时才会为 true,可以通过 `delay` 参数设置延迟的时长
218
+ Therefore, the loading component also provides the `pastDelay` parameter, which will only be true when it exceeds the set delay display. You can set the delay duration through the `delay` parameter.
213
219
 
214
220
  ```tsx
215
221
  function Loading({ error, pastDelay }) {
@@ -223,7 +229,7 @@ function Loading({ error, pastDelay }) {
223
229
  }
224
230
  ```
225
231
 
226
- `delay` 的默认值为 `200ms`,可以通过 `loadable` 中的 `delay` 来设置延迟展示的时间
232
+ The default value of `delay` is `200ms`, you can set the delay display time through `delay` in `loadable`.
227
233
 
228
234
  ```tsx
229
235
 
@@ -239,11 +245,11 @@ function App(){
239
245
  }
240
246
  ```
241
247
 
242
- #### 增加超时状态
248
+ #### Add Timeout State
243
249
 
244
- 有时因为网络的原因,从而导致微前端子应用加载失败,从而导致一直展示 loading 的状态,这对于用户而言非常糟糕,因为他们不知道合适才会获得具体的响应,他们是否需要刷新页面,通过增加超时状态可以很好的解决该问题。
250
+ Sometimes because of the network, the micro-front-end sub-application fails to load, resulting in the loading state being displayed all the time, which is very bad for users, because they don't know the right response to get a specific response, whether they need to refresh the page, by Increasing the timeout state can solve this problem well.
245
251
 
246
- loading 组件在超时时会获得 `timeOut` 参数,当微前端应用加载超时时将会获得 `timeOut` 属性值为 true
252
+ The loading component will get the `timeOut` parameter when timeout, when the micro frontend application loads timeout, it will get the `timeOut` property value of true.
247
253
 
248
254
  ```tsx
249
255
  function Loading({ error, timeOut, pastDelay }) {
@@ -259,7 +265,7 @@ function Loading({ error, timeOut, pastDelay }) {
259
265
  }
260
266
  ```
261
267
 
262
- 超时状态是关闭的,可以通过在 `loadable` 中设置 `timeout` 参数开启
268
+ The timeout state is off and can be enabled by setting the `timeout` parameter in `loadable`:
263
269
 
264
270
  ```tsx
265
271
 
@@ -1,14 +1,14 @@
1
1
  ---
2
2
  sidebar_position: 4
3
- title: 主子应用通信
3
+ title: Communicate
4
4
  ---
5
- # 主子应用通信
5
+ # Communicate
6
6
 
7
- ## props 通信
7
+ ## Props
8
8
 
9
- Modern.js 中,会将子应用包裹成一个 React 组件,直接通过给 React 组件传递 `props` 即可实现主应用和子应用通信的目的。
9
+ In Modern.js, the sub-application will be wrapped into a React component, and the purpose of communicating between the main application and the sub-application can be achieved directly by passing'props' to the React component.
10
10
 
11
- ```tsx title=主应用:App.tsx
11
+ ```tsx title=Main:App.tsx
12
12
  function App() {
13
13
  const { MApp } = useModuleApps();
14
14
 
@@ -20,21 +20,21 @@ function App() {
20
20
  }
21
21
  ```
22
22
 
23
- ```tsx title=子应用:App.tsx
23
+ ```tsx title=Main:App.tsx
24
24
  function App(props) {
25
25
  console.log(props);
26
26
  return ...
27
27
  }
28
28
  ```
29
29
 
30
- 子应用将会打印 `{count: 0}`,目前尚未支持子应用渲染响应式,及时在主应用上更改 `count` 的数据也不会触发视图更新
30
+ The child application will print `{count: 0}`. Currently, child application rendering responsiveness is not supported. Changing the data of'count 'on the main application in time will not trigger a view update
31
31
 
32
- ## channel 通信
32
+ ## channel
33
33
 
34
- [Garfish.channel](https://www.garfishjs.org/api/channel) 用于应用间的通信。它是 `EventEmitter2` 的实例
34
+ [Garfish.channel] (https://www.garfishjs.org/api/channel) Used for communication between applications. It is an instance of `EventEmitter2`.
35
35
 
36
36
  ```ts
37
- // 子应用监听登录事件
37
+ // sub app listen login event
38
38
  const App = () => {
39
39
  const handleLogin = userInfo => {
40
40
  console.log(`${userInfo.name} has login`);
@@ -48,7 +48,7 @@ const App = () => {
48
48
  });
49
49
  };
50
50
 
51
- // 主应用触发监听事件
51
+ // main app emit login event
52
52
  api.getLoginInfo.then(res => {
53
53
  if (res.code === 0) {
54
54
  window.Garfish.channel.emit('login', res.data);
@@ -1,26 +1,26 @@
1
1
  ---
2
2
  sidebar_position: 5
3
- title: 混合技术栈
3
+ title: Mixed Stack
4
4
  ---
5
- # 混合技术栈
5
+ # Mixed Stack
6
6
 
7
- Modern.js 微前端方案是基于 [Garfish](https://garfishjs.org/) 封装的,提供了一些更开箱即用的使用方式。
7
+ The Modern.js micro frontend scheme is based on the [Garfish](https://garfishjs.org/) package and provides some more out-of-the-box usage.
8
8
 
9
- 当你的主应用和子应用不全是 Modern.js 应用的时候,可以参考这篇文档。
9
+ When your main application and sub-application are not all Modern.js applications, you can refer to this document.
10
10
 
11
- 1. 子应用是 **Modern.js**,主应用使用的原生 Garfish 微前端。
12
- 2. 主应用是 **Modern.js**,子应用有的是其它技术栈。
11
+ 1. The sub-app is **Modern.js**, the native Garfish micro frontend used by the main app.
12
+ 2. The main application is **Modern.js**, and some sub-applications have other technology stacks.
13
13
 
14
- ## 子应用是 Modern.js
14
+ ## Modern.js Sub App
15
15
 
16
- **Modern.js** 子应用编译后会生成标准的 [Garfish 子应用导出](https://www.garfishjs.org/guide/start#2%E5%AF%BC%E5%87%BA-provider-%E5%87%BD%E6%95%B0)
17
- 所以可以直接接入标准的微前端主应用。
16
+ **Modern.js** subapps compile to generate a standard [Garfish subapp export](https://www.garfishjs.org/guide/start#2%E5%AF%BC%E5%87%BA-provider-%E5%87%BD%E6%95%B0).
17
+ So you can directly access the standard micro frontend main application.
18
18
 
19
- :::info
20
- 子应用是 **Modern.js**,主应用使用的原生 Garfish 微前端时,**子应用调试模式** 不可用。
21
19
 
20
+ :::info
21
+ The child application is **Modern.js**, when the main application uses the native Garfish micro frontend, the **child application debugging mode** is not available.
22
22
  :::
23
23
 
24
- ## 主应用是 Modern.js
24
+ ## Modern.js Main App
25
25
 
26
- 主应用是 **Modern.js**,子应用用的其它技术栈。子应用按照 [Garfish 子应用标准](https://www.garfishjs.org/guide/demo/react) 开发即可。
26
+ The Main App is **Modern.js**, and other technology stacks used by sub-applications. Sub-App can be developed according to [Garfish Sub-Application Standard](https://www.garfishjs.org/guide/demo/react).
@@ -1,23 +1,22 @@
1
1
  ---
2
2
  sidebar_position: 6
3
- title: 自动生成 Actions
3
+ title: Automatically Actions
4
4
  ---
5
- # 自动生成 Actions
5
+ # Automatically Generated Actions
6
6
 
7
- [快速上手](/guides/topic-detail/model/quick-start) 中,我们实现最简单的计数器 Model 也需要 10 行代码。
8
- 实际上,Modern.js 支持根据声明的 `state` 类型,自动生成常用的 Actions,从而简化模板代码量。当前支持的类型有:
7
+ In the [Quick Start](/guides/topic-detail/model/quick-start), we implemented the simplest counter model, which still required 10 lines of code. In fact, Modern.js supports automatically generating commonly used actions based on the declared `state` type, which reduces the amount of boilerplate code. The currently supported types are:
9
8
 
10
- - 原始数据类型
11
- - 数组类型
12
- - 简单对象类型(Plain Object
9
+ - Primitive data types
10
+ - Array types
11
+ - Simple object types (Plain Object)
13
12
 
14
- ## 原始数据类型
13
+ ## Primitive data types
15
14
 
16
15
  ```ts
17
16
  const countModel = model('count').define({ state: 1 });
18
17
  ```
19
18
 
20
- 如上我们仅用一行就完成了一个简单的 `countModel`。使用 Model 的示例代码如下:
19
+ As shown above, we only need one line to create a simple `countModel`. Here's an example code that uses the model:
21
20
 
22
21
  ```tsx
23
22
  function Counter() {
@@ -30,9 +29,9 @@ function Counter() {
30
29
  }
31
30
  ```
32
31
 
33
- ## 数组类型
32
+ ## Array Types
34
33
 
35
- State 为数组类型时,自动生成 Actions 的示例代码如下:
34
+ When the state is an array type, an example code for automatically generating actions is shown below:
36
35
 
37
36
  ```ts
38
37
  const countModel = model('count').define({ state: [] });
@@ -52,11 +51,11 @@ function Counter() {
52
51
  }
53
52
  ```
54
53
 
55
- 我们可以使用 JavaScript Array 对象的方法,修改 State。
54
+ We can use the methods of the JavaScript Array object to modify the state.
56
55
 
57
- ## 简单对象类型
56
+ ## Simple Object Types
58
57
 
59
- State 为简单对象类型时,自动生成 Actions 的示例代码如下:
58
+ When the state is a simple object type, an example code for automatically generating actions is shown below:
60
59
 
61
60
  ```ts
62
61
  const countModel = model('count').define({
@@ -78,15 +77,13 @@ function Counter() {
78
77
  }
79
78
  ```
80
79
 
81
- 根据 `a`、`b`、`c` 三个不同的字段分别生成 `setA`、`setB`、`setC` 三个 Actions。
80
+ Three different fields `a`, `b`, and `c` each generate `setA`, `setB`, and `setC` actions, respectively.
82
81
 
83
82
  :::info
84
- 当用户自定义的 Action Modern.js 自动生成的 Action 名字一致时,用户自定义的 Action 优先级更高。例如,
85
- 在 `countModel` 中已经自定义 `setA` 这个 Action,调用 `actions.setA()` 时,最终执行的是用户自定义的 `setA`。
86
-
83
+ When the user-defined action and the action automatically generated by Modern.js have the same name, the user-defined action takes precedence. For example, if `setA` action is already defined in `countModel`, calling `actions.setA()` executes the user-defined `setA`.
87
84
  :::
88
85
 
89
- :::info 补充信息
90
- 相关 API 的更多介绍,请参考[这里](/apis/app/runtime/model/auto-actions)
91
-
86
+ :::info Additional Information
87
+ For more information on related APIs, please refer to [here](/apis/app/runtime/model/auto-actions).
92
88
  :::
89
+
@@ -1,32 +1,32 @@
1
1
  ---
2
2
  sidebar_position: 4
3
- title: 衍生状态
3
+ title: Derived State
4
4
  ---
5
- # 衍生状态
6
5
 
7
- 一些场景中,组件需要对 Model 中的 State 进行进一步计算,才能在组件中使用,这部分逻辑可以直接写在组件内部,也可以通过 Model 的衍生状态实现。
8
- 衍生状态定义在 Model 中的 `computed` 字段下。根据依赖的 Model 的不同、返回类型的不同,衍生状态的定义方法可以分为以下 3 种。
6
+ # Derived State
9
7
 
10
- ## 只依赖自身的 State
8
+ In some scenarios, components need to perform further calculations on the State in Model before they can be used in the components. This logic can be directly written in the component or implemented through derived states in Model. Derived states are defined under the `computed` field in the Model. Depending on the dependencies of the Model and the return type, there are three ways to define derived states.
11
9
 
12
- 衍生状态只依赖当前 Model State,State 会作为第一个参数,传入衍生状态的定义函数中。
10
+ ## Only Depend on the Model's Own State
13
11
 
14
- 例如, todo 应用的 State `items` `filter`,`filter` 用于过滤当前页面显示的 todo 项,所以我们定义了一个 `visibleTodos` 的衍生状态可以直接在组件中使用。示例代码如下:
12
+ The derived state only depends on the current Model's State, which is passed as the first parameter to the derived state's definition function.
13
+
14
+ For example, the todo application has `items` and `filter` in its State, and `filter` is used to filter the todo items displayed on the current page. Therefore, we define a `visibleTodos` derived state that can be directly used in the component. The sample code is as follows:
15
15
 
16
16
  ```ts
17
17
  /**
18
- * 假设 todo item 结构如下:
18
+ * Assuming the structure of the todo item is as follows:
19
19
  {
20
20
  id: string; // id
21
- text: string; // todo 事项
22
- completed: boolean; // 完成状态:0 代表未完成,1 代表完成
21
+ text: string; // todo
22
+ completed: boolean;
23
23
  }
24
24
  **/
25
25
 
26
26
  const todoModel = model('todo').define({
27
27
  state: {
28
28
  items: [],
29
- filter: 'ALL', // ALL: 显示全部;COMPLETED:显示完成的事项;ACTIVE:显示未完成的事项
29
+ filter: 'ALL', // ALL: show all;COMPLETED:show completed;ACTIVE:show active
30
30
  },
31
31
  computed: {
32
32
  visibleTodos: state => {
@@ -45,7 +45,7 @@ const todoModel = model('todo').define({
45
45
  });
46
46
  ```
47
47
 
48
- 衍生状态最终会和 Model State 进行合并,因此,可以通过 Model State 对象访问到衍生状态,例如,`visibleTodos` 在组件内的使用方式如下:
48
+ Derived state will eventually be merged with the Model's State, so the derived state can be accessed through the Model's State object. For example, the `visibleTodos` can be used in the component as follows:
49
49
 
50
50
  ```ts
51
51
  function Todo() {
@@ -63,15 +63,17 @@ function Todo() {
63
63
  }
64
64
  ```
65
65
 
66
- ## 依赖其他 Model State
66
+ ## Dependent State from Other Models
67
+
68
+ In addition to depending on the current model's state, derived states may also depend on the state of other models. In this case, the definition format for the derived state is:
67
69
 
68
- 除了依赖当前 Model 的 State,衍生状态还依赖其他 Model 的 State,这时候衍生状态的定义格式为:
69
70
 
70
71
  ```ts
71
72
  [stateKey]: [...depModels, (selfState, ...depModels) => computedState]
72
73
  ```
73
74
 
74
- 下面的示例,演示了 `barModel` 的衍生状态 `combinedValue` 是如何依赖 `fooModel` State 的。
75
+ The following example demonstrates how the derived state `combinedValue` of `barModel` depends on the state of `fooModel`.
76
+
75
77
 
76
78
  ```ts
77
79
  const fooModel = model('foo').define({
@@ -93,16 +95,17 @@ const barModel = model('bar').define({
93
95
  });
94
96
  ```
95
97
 
96
- ## 函数类型的衍生状态
98
+ ## Derived State with Function Type
97
99
 
98
- 衍生状态还可以返回一个函数。这时候衍生状态的定义格式为:
100
+ Derived states can also return a function. In this case, the definition format for the derived state is:
99
101
 
100
102
  ```ts
101
- [stateKey]: (state) => (...args) => computedState // 只依赖自身的 state
102
- [stateKey]: [...depModels, (selfState, ...depModels) => (...args) => computedState] // 依赖其他 Model state
103
+ [stateKey]: (state) => (...args) => computedState // Only relies on its own state
104
+ [stateKey]: [...depModels, (selfState, ...depModels) => (...args) => computedState] // Relies on the state of other models
103
105
  ```
104
106
 
105
- 假设,todo 应用的 state 不存储 `filter` 状态,而是直接在组件中使用,那么 `visibleTodos` 可以是一个函数类型的值,这个函数在组件中使用的时候,接收 `filter` 参数。如下所示:
107
+ Assuming the `filter` state is not stored in the state of the todo app, but is instead used directly in the component, `visibleTodos` can be a function type value. This function receives the `filter` parameter when used in the component, as shown below:
108
+
106
109
 
107
110
  ```ts
108
111
  const todoModel = model('todo').define({
@@ -126,11 +129,11 @@ const todoModel = model('todo').define({
126
129
  });
127
130
 
128
131
  function Todo(props) {
129
- // filter 状态通过 props 传入
132
+ // use props pass filter
130
133
  const { filter } = props;
131
134
  const [state, actions] = useModel(todoModel);
132
135
 
133
- // 计算得到最终要显示的 todos
136
+ // get final todos
134
137
  const todos = state.visibleTodos(filter);
135
138
 
136
139
  return (
@@ -145,7 +148,6 @@ function Todo(props) {
145
148
  }
146
149
  ```
147
150
 
148
- :::info 更多参考
149
- [使用 Model](/guides/topic-detail/model/computed-state)
150
-
151
+ :::info Additional Reference
152
+ [Using Models](/guides/topic-detail/model/computed-state)
151
153
  :::
@@ -1,12 +1,13 @@
1
1
  ---
2
2
  sidebar_position: 2
3
- title: 创建 Model
3
+ title: Define Model
4
4
  ---
5
- # 创建 Model
5
+ # Define a Model
6
6
 
7
- 上一节的计数器应用中,我们简单演示了如何创建一个 Model。本节我们将详细介绍 Model 的创建方法。
7
+ In the previous section, we demonstrated how to create a Model for a simple counter application. In this section, we will provide a detailed introduction on how to create a Model.
8
+
9
+ We can create a Model using the `model` API. For example, `model('foo')` creates a Model named `foo`. We can define the State and Actions included in the Model by calling the `define` function returned by `model('foo')`:
8
10
 
9
- 通过 `model` API 创建 Model,例如,`model('foo')` 表示要创建一个名为 `foo` 的 Model,通过调用 `model('foo')` 返回的 `define` 函数,定义 Model 包含的 State、Actions 等:
10
11
 
11
12
  ```js
12
13
  import { model, useModel } from '@modern-js/runtime/model';
@@ -24,14 +25,14 @@ const fooModel = model('foo').define({
24
25
  ```
25
26
 
26
27
  :::info
27
- - Model 中的 Action 中不能包含有副作用的逻辑,如请求 HTTP 接口,访问 localStorage 等。
28
- - `setValue` 内部直接修改了入参 State,看起来是违反了纯函数的定义,实际上 Reduck 内部使用 [immer](https://github.com/immerjs/immer) 来修改不可变对象,保证了这种写法不会影响对象的不可变性,所以 `setValue` 仍然是一个纯函数。当然,你也可以直接在 Action 中返回一个新对象,不过这样的写法会更加复杂一些。
28
+ - Actions in the Model cannot contain side-effect logic, such as requesting HTTP interfaces, accessing localStorage, etc.
29
+ - `setValue` directly modifies the input State, which seems to violate the definition of a pure function. However, Reduck uses [immer](https://github.com/immerjs/immer) internally to modify immutable objects, ensuring that this approach does not affect the immutability of the object, so `setValue` is still a pure function. Of course, you can also return a new object directly in the Action, but this approach will be more complex.
29
30
 
30
31
  :::
31
32
 
32
- `define` 接收的参数,只是对 Model 原始结构的描述:内部定义的 StateActions 等。`define` 返回值 `fooModel` 才是真正创建得到的 Model 对象。例如,虽然 `setValue`,有 2 个参数,但是当真正调用 `setValue` 这个 Action 时,我们只需要传入 `payload` 一个参数,因为我们调用的是 `fooModel` 上的 Action 方法,而不是 Model 原始结构上描述的 Action 方法。详细参考[使用 Model](/guides/topic-detail/model/use-model)
33
+ The `define` function only describes the original structure of the Model, including the internally defined State, Actions, and so on. The `fooModel` returned by `define` is the actual Model object that is created. For example, although `setValue` has two parameters, when we call the `setValue` Action, we only need to pass in the `payload` parameter, because we are calling the Action method on `fooModel`, not the one described in the original structure of the Model. For more information, please refer to [Using Models](/guides/topic-detail/model/use-model).
33
34
 
34
- `define` 除了接收对象类型的参数,还可以接收函数类型的参数。例如:
35
+ In addition to object-type parameters, `define` can also receive function-type parameters. For example:
35
36
 
36
37
  ```js
37
38
  import { model, useModel } from '@modern-js/runtime/model';
@@ -50,17 +51,16 @@ const fooModel = model('foo').define((context, utils) => {
50
51
  });
51
52
  ```
52
53
 
53
- 通过函数定义 Model 时,函数内部会自动传入 `context`、`utils` 2 个参数,`context` Reduck 的上下文对象,可以获取到 `store` 对象,`utils` 提供了一组工具函数,方便实现 Model 通信等复杂功能需求。
54
+ When defining a Model using a function, the function automatically receives two parameters: `context` and `utils`. `context` is the context object of Reduck, which can obtain the `store` object, and `utils` provides a set of utility functions to facilitate the implementation of complex features such as Model communication.
54
55
 
55
- Model 支持复制。例如:
56
+ Models support duplication. For example:
56
57
 
57
58
  ```ts
58
59
  const barModel = fooModel('bar');
59
60
  ```
60
61
 
61
- barModel 是基于 fooModel 创建出一个的新的 Model 对象,类比面向对象编程语言中的概念,barModel fooModel 是基于同一个类(Class)创建出的 2 个实例对象。当两个模块的状态管理逻辑相同,例如一个页面中的两个 tab 模块,使用的数据的结构和逻辑相同,区别只是从不同的接口获取数据,那么可以通过 Model 复制的方式,创建 2 个不同的 Model 对象。
62
-
63
- :::info 补充信息
64
- 本节涉及的 API 的详细定义,请参考[这里](/apis/app/runtime/model/model_)。
62
+ `barModel` is a new Model object created based on `fooModel`. Analogous to the concepts in object-oriented programming languages, `barModel` and `fooModel` are two instance objects created based on the same class. When the state management logic of two modules is the same, for example, two tab modules in the same page use the same data structure and logic, but the difference is that they obtain data from different interfaces, then you can create two different Model objects by duplicating the Model.
65
63
 
64
+ :::info Additional Information
65
+ For detailed definitions of the APIs mentioned in this section, please refer to [here](/apis/app/runtime/model/model_).
66
66
  :::