You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: docs/docs/docs/max/data-flow.en-US.md
+34-35
Original file line number
Diff line number
Diff line change
@@ -1,31 +1,30 @@
1
1
---
2
2
order: 5
3
3
toc: content
4
-
translated_at: '2024-03-17T09:24:49.645Z'
4
+
translated_at: '2024-03-18T00:49:20.502Z'
5
5
---
6
6
7
-
```markdown
8
7
# Data Stream
9
8
10
-
`@umi/max` has a built-in **data stream management** [plugin](https://github.com/umijs/umi/blob/master/packages/plugins/src/model.ts), which is a lightweight data management solution based on the `hooks` paradigm. It allows managing global shared data in Umi projects.
9
+
`@umi/max` has a built-in **data flow management**[plugin](https://github.com/umijs/umi/blob/master/packages/plugins/src/model.ts), which is a lightweight data management solution based on the `hooks` paradigm. It can be used to manage global shared data in Umi projects.
11
10
12
11
## Getting Started
13
12
14
13
### Creating a Model
15
14
16
-
The data stream management plugin adopts a conventional directory structure. We agree that you can include Model files in the `src/models`, `src/pages/xxxx/models/` directory, and `src/pages/xxxx/model.{js,jsx,ts,tsx}` files.
17
-
Model files can use `.(tsx|ts|jsx|js)` four suffix formats, the **namespace** generation rule is as follows.
15
+
The data flow management plugin adopts a conventional directory structure. We agree that Model files can be introduced in the `src/models`, `src/pages/xxxx/models/`directories, and in`src/pages/xxxx/model.{js,jsx,ts,tsx}` files.
16
+
Model files can have one of four suffix formats: `.(tsx|ts|jsx|js)`. The**namespace** generation rule is as follows.
18
17
19
-
| Path | Namespace | Explanation |
18
+
| Path | Namespace |Description|
20
19
| :--- |:--- | :--- |
21
-
| `src/models/count.ts` | `count` | `src/models` directory does not support nested directory definition model |
20
+
|`src/models/count.ts`|`count`|Defining model in `src/models` directory does not support directory nesting|
| `src/pages/pageB/models/fruit/apple.ts` | `pageB.fruit.apple` | `pages/xxx/models` under model definition supports nested definition |
23
+
|`src/pages/pageB/models/fruit/apple.ts`|`pageB.fruit.apple`|Definition in `pages/xxx/models` supports nested models|
25
24
26
-
A Model is essentially a custom `hooks` that doesn't involve any "black magic" that users need to worry about.
25
+
A Model is essentially a custom `hook` with no "black magic" for the user to worry about.
27
26
28
-
When you need to get the global data in the Model, you can call its namespace. For example, for the Model file `userModel.ts`, its namespace is `userModel`.
27
+
When we need to access the global data in the Model, we can call the namespace directly. For example, for a Model file `userModel.ts`, its namespace is `userModel`.
29
28
30
29
Write a function with a default export:
31
30
@@ -40,13 +39,13 @@ export default function Page() {
40
39
};
41
40
```
42
41
43
-
This is a Model. The plugin's job is to turn the state or data within it into **global data**, so when different components use this Model, they get the same set of state or data.
42
+
This is a Model. What the plugin does is turn the state or data in it into **global data**, providing the same state or data to different components using that Model.
44
43
45
44
:::info{title=💡}
46
-
The Model file needs to default export a function, which defines a `hook`. Files that do not comply with this specification will be filtered out and cannot be called by namespace.
45
+
The Model file needs to export a function by default, which defines a `hook`. Files that do not conform to this standard will be filtered out and cannot be called by namespace.
47
46
:::
48
47
49
-
The Model can use other `hooks`, taking a counter as an example:
48
+
It's allowed to use other `hooks` in a Model. Take a counter as an example:
50
49
51
50
```ts
52
51
// src/models/counterModel.ts
@@ -62,7 +61,7 @@ export default function Page() {
62
61
};
63
62
```
64
63
65
-
In project practice, we usually need to request backend interfaces to obtain the required data. Now let's extend the previous example of getting user information:
64
+
In real-world projects, we usually need to request backend interfaces to obtain the data we need. Now let's expand the previous example of fetching user information:
66
65
67
66
```ts
68
67
// src/models/userModel.ts
@@ -110,9 +109,9 @@ export default function Page() {
110
109
};
111
110
```
112
111
113
-
### Using Model
112
+
### Using a Model
114
113
115
-
Now, if you want to use a global Model in a specific component. Taking user information as an example, just call the `useModel` hook function:
114
+
Now, you want to use a global Model in a certain component. Taking the user information as an example, you only need to call the `useModel` hook function:
116
115
117
116
```tsx
118
117
// src/components/Username/index.tsx
@@ -127,17 +126,17 @@ export default function Page() {
127
126
}
128
127
```
129
128
130
-
In this case, the `useModel()` method's parameter is the Model's **namespace**.
129
+
The `useModel()` method's argument is the Model's **namespace**.
131
130
132
131
:::info{title=💡}
133
-
If you use VSCode as the IDE for developing Umi projects, it is recommended to use in conjunction with the [@umijs/plugin-model](https://marketplace.visualstudio.com/items?itemName=litiany4.umijs-plugin-model) plugin. It allows you to quickly jump to the file that defines the Model:
132
+
If you use VSCode as the IDE for developing Umi projects, it's recommended to use it in conjunction with the [@umijs/plugin-model](https://marketplace.visualstudio.com/items?itemName=litiany4.umijs-plugin-model) plugin. It allows you to quickly jump to the file that defines the Model:
The `useModel()` method can accept an optional second parameter. When the component only needs to use part of the Model's parameters and is not interested in the changes of other parameters, a function can be passed in for filtering. Taking the operation buttons of the counter as an example:
139
+
The `useModel()` method can accept an optional second parameter. When a component only needs to use part of the Modeland is not interested in changes to other parameters, you can pass in a function to filter. Take the operation button for the counter as an example:
141
140
142
141
```tsx
143
142
// src/components/CounterActions/index.tsx
@@ -158,17 +157,17 @@ export default function Page() {
158
157
};
159
158
```
160
159
161
-
The above component is not concerned with the `counter` value in the counter Model, only needing to use the `increment()` and `decrement()` methods provided by the Model. Thus, we passed in a function as the second parameter of the `useModel()` method, whose return value will serve as the `useModel()` method's return value.
160
+
The above component is not interested in the `counter` value in the counter Model, and only needs to use the `increment()` and `decrement()` methods provided by the Model. Therefore, we passed in a function as the second argument for the `useModel()` method, and its return value will be used as the return value of the `useModel()` method.
162
161
163
-
This way, we filter out the frequently changing `counter` value, avoiding performance loss caused by repeated rendering of the component.
162
+
This way, we have filtered out the frequently changing `counter` value, avoiding the performance loss caused by the component's repetitive rendering.
164
163
165
164
## Global Initial State
166
165
167
-
`@umi/max` has a built-in **global initial state management**[plugin](https://github.com/umijs/umi/blob/master/packages/plugins/src/initial-state.ts), allowing you to quickly build and obtain the global initial state of Umi projects within components.
166
+
`@umi/max` has a built-in **global initial state management**[plugin](https://github.com/umijs/umi/blob/master/packages/plugins/src/initial-state.ts), which allows you to quickly build and access the global initial state of Umi projects within components.
168
167
169
168
The global initial state is a special Model.
170
169
171
-
The global initial state is created at the very beginning of the entire Umi project. Write the `getInitialState()`export method in `src/app.ts`, whose return value will become the global initial state. For example:
170
+
The global initial state is created at the very beginning of the entire Umi project. Write the export method `getInitialState()` in `src/app.ts`, and its return value will become the global initial state. For example:
172
171
173
172
```ts
174
173
// src/app.ts
@@ -180,7 +179,7 @@ export async function getInitialState() {
180
179
}
181
180
```
182
181
183
-
Now, various plugins and components you define can directly obtain this global initial state through `useModel('@@initialState')`, as shown below:
182
+
Now, various plugins and your defined components can directly access this global initial state through `useModel('@@initialState')` as shown below:
184
183
185
184
```tsx
186
185
import { useModel } from'umi';
@@ -192,30 +191,30 @@ export default function Page() {
192
191
};
193
192
```
194
193
195
-
| Object Property | Type |Introduction|
194
+
| Object Property | Type |Description|
196
195
| --- | --- | --- |
197
196
|`initialState`|`any`| The return value of the exported `getInitialState()` method |
198
-
|`loading`|`boolean`| Whether the `getInitialState()` or `refresh()` method is in progress. Before the initial state is first obtained, the rendering of other parts of the page will be **blocked**|
199
-
|`error`|`Error`| If the exported `getInitialState()` method throws an error, the error message |
197
+
|`loading`|`boolean`| Whether the `getInitialState()` or `refresh()` method is in progress. Before the initial state is obtained for the first time, the rendering of other parts of the page will be **blocked**|
198
+
|`error`|`Error`| If an error occurs while running the exported `getInitialState()` method, the error message |
200
199
|`refresh`|`() => void`| Re-execute the `getInitialState` method and obtain a new global initial state |
201
-
|`setInitialState`|`(state: any) => void`| Manually set the value of `initialState`, after manual setting, `loading` will be set to `false`|
200
+
|`setInitialState`|`(state: any) => void`| Manually set the value of `initialState`, after manually setting it, `loading` will be set to `false`|
202
201
203
-
## Qiankun Parent-Child Application Communication
202
+
## Qiankun Parent-Child Communication
204
203
205
-
`@umi/max` has a built-in **Qiankun Microfrontend**[plugin](https://github.com/umijs/umi/blob/master/packages/plugins/src/qiankun.ts), when using the data stream plugin, it allows micro-applications to obtain the parent application's data Model passed to the child application through the `useModel('@@qiankunStateFromMaster')` method, thereby achieving communication between parent and child applications.
204
+
`@umi/max` has a built-in **Qiankun Micro-Frontend**[plugin](https://github.com/umijs/umi/blob/master/packages/plugins/src/qiankun.ts), which allows micro-applications to obtain the data Model passed from the parent application through the `useModel('@@qiankunStateFromMaster')` method, thereby achieving communication between parent and child applications.
206
205
207
-
For specific usage methods, please refer to the section on parent-child application communication in microfrontend.
206
+
For specific usage, please refer to the [micro-frontend's parent-child communication chapter](./micro-frontend#parent-child-communication).
208
207
209
208
## API
210
209
211
210
### `useModel`
212
211
213
-
`useModel()` is a hook function that provides the capability to use Model. It accepts two parameters:
212
+
`useModel()` is a hook function that provides the ability to use a Model. It accepts two parameters:
214
213
215
-
| Parameter | Type |Introduction|
214
+
| Parameter | Type |Description|
216
215
| --- | --- | --- |
217
216
|`namespace`|`String`| The namespace of the Model file |
218
-
|`updater`|`(model: any) => any`|An optional parameter. Pass in a function, whose return value is the Model state or data currently needed in the component, and serves as the return value of the `useModel()` method. It is of significant importance for optimizing component performance. |
217
+
|`updater`|`(model: any) => any`|Optional parameter. Passing in a function, the return value of which is the Model state or data currently needed by the component, and it serves as the return value of the `useModel()` method. It plays a significant role in optimizing component performance. |
0 commit comments