@modern-js/main-doc 2.6.0 → 2.8.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/CHANGELOG.md +17 -0
- package/README.md +2 -2
- package/en/apis/app/commands.mdx +2 -0
- package/en/apis/app/hooks/config/upload.mdx +10 -0
- package/en/apis/app/runtime/model/connect.mdx +1 -1
- package/en/apis/app/runtime/model/model_.mdx +1 -1
- package/en/apis/app/runtime/model/use-model.mdx +1 -1
- package/en/apis/app/runtime/web-server/hook.mdx +2 -2
- package/en/apis/app/runtime/web-server/middleware.mdx +33 -9
- package/en/components/enable-bff.mdx +4 -4
- package/en/components/init-rspack-app.mdx +7 -0
- package/en/configure/app/bff/enable-handle-web.mdx +24 -0
- package/en/configure/app/server/enable-framework-ext.mdx +1 -1
- package/en/configure/app/server/ssr.mdx +18 -0
- package/en/guides/advanced-features/bff/_category_.json +1 -1
- package/en/guides/advanced-features/eslint.mdx +30 -32
- package/en/guides/advanced-features/low-level.mdx +1 -1
- package/en/guides/advanced-features/rspack-start.mdx +13 -17
- package/en/guides/advanced-features/ssr.mdx +210 -5
- package/en/guides/advanced-features/web-server.mdx +87 -20
- package/en/guides/{css/tailwindcss.mdx → basic-features/css.mdx} +60 -3
- package/en/guides/basic-features/env-vars.mdx +4 -0
- package/en/guides/concept/builder.mdx +1 -1
- package/en/guides/topic-detail/framework-plugin/extend.mdx +20 -19
- package/en/guides/topic-detail/framework-plugin/hook-list.mdx +156 -155
- package/en/guides/topic-detail/framework-plugin/hook.mdx +58 -43
- package/en/guides/topic-detail/framework-plugin/implement.mdx +47 -49
- package/en/guides/topic-detail/framework-plugin/introduction.mdx +22 -23
- package/en/guides/topic-detail/framework-plugin/plugin-api.mdx +13 -13
- package/en/guides/topic-detail/framework-plugin/relationship.mdx +30 -30
- package/en/guides/topic-detail/generator/plugin/develop.mdx +1 -1
- package/en/guides/topic-detail/micro-frontend/c01-introduction.mdx +17 -17
- package/en/guides/topic-detail/micro-frontend/c02-development.mdx +76 -78
- package/en/guides/topic-detail/micro-frontend/c03-main-app.mdx +57 -51
- package/en/guides/topic-detail/micro-frontend/c04-communicate.mdx +11 -11
- package/en/guides/topic-detail/micro-frontend/c05-mixed-stack.mdx +13 -13
- package/en/guides/topic-detail/model/auto-actions.mdx +18 -21
- package/en/guides/topic-detail/model/computed-state.mdx +27 -25
- package/en/guides/topic-detail/model/define-model.mdx +14 -14
- package/en/guides/topic-detail/model/faq.mdx +12 -13
- package/en/guides/topic-detail/model/manage-effects.mdx +43 -52
- package/en/guides/topic-detail/model/model-communicate.mdx +47 -45
- package/en/guides/topic-detail/model/performance.mdx +22 -23
- package/en/guides/topic-detail/model/quick-start.mdx +29 -28
- package/en/guides/topic-detail/model/redux-integration.mdx +7 -7
- package/en/guides/topic-detail/model/test-model.mdx +11 -11
- package/en/guides/topic-detail/model/typescript-best-practice.mdx +16 -15
- package/en/guides/topic-detail/model/use-model.mdx +40 -45
- package/en/guides/topic-detail/model/use-out-of-modernjs.mdx +16 -16
- package/en/guides/troubleshooting/cli.mdx +2 -2
- package/en/tutorials/first-app/c03-css.mdx +1 -1
- package/package.json +5 -5
- package/zh/apis/app/commands.mdx +2 -0
- package/zh/apis/app/hooks/config/upload.mdx +12 -2
- package/zh/apis/app/runtime/model/connect.mdx +1 -1
- package/zh/apis/app/runtime/model/model_.mdx +1 -1
- package/zh/apis/app/runtime/model/use-model.mdx +1 -1
- package/zh/apis/app/runtime/web-server/hook.mdx +2 -4
- package/zh/apis/app/runtime/web-server/middleware.mdx +30 -10
- package/zh/apis/monorepo/commands/gen-release-note.mdx +3 -3
- package/zh/components/enable-bff.mdx +4 -4
- package/zh/components/init-rspack-app.mdx +7 -0
- package/zh/components/release-note.mdx +1 -1
- package/zh/configure/app/bff/enable-handle-web.mdx +24 -0
- package/zh/configure/app/server/enable-framework-ext.mdx +1 -1
- package/zh/configure/app/server/ssr.mdx +19 -1
- package/zh/guides/advanced-features/bff/_category_.json +1 -1
- package/zh/guides/advanced-features/rspack-start.mdx +13 -17
- package/zh/guides/advanced-features/ssr.mdx +210 -4
- package/zh/guides/advanced-features/web-server.mdx +81 -16
- package/zh/guides/{css/tailwindcss.mdx → basic-features/css.mdx} +60 -3
- package/zh/guides/basic-features/env-vars.mdx +4 -0
- package/zh/guides/concept/builder.mdx +1 -1
- package/zh/guides/topic-detail/changesets/github.mdx +2 -2
- package/zh/guides/topic-detail/changesets/release-note.mdx +1 -1
- package/zh/guides/topic-detail/framework-plugin/plugin-api.mdx +2 -2
- package/zh/guides/topic-detail/generator/plugin/develop.mdx +1 -1
- package/zh/guides/topic-detail/model/faq.mdx +1 -1
- package/zh/guides/topic-detail/model/manage-effects.mdx +1 -1
- package/zh/guides/topic-detail/model/model-communicate.mdx +1 -1
- package/zh/guides/topic-detail/model/performance.mdx +1 -1
- package/zh/guides/topic-detail/model/quick-start.mdx +2 -2
- package/zh/guides/topic-detail/model/use-model.mdx +3 -3
- package/zh/tutorials/first-app/c03-css.mdx +1 -1
- package/en/guides/css/_category_.json +0 -5
- package/en/guides/css/css-in-js.mdx +0 -40
- package/en/guides/css/css-modules.mdx +0 -87
- package/en/guides/css/less-sass.mdx +0 -17
- package/en/guides/css/postcss.mdx +0 -79
- package/zh/guides/css/_category_.json +0 -5
- package/zh/guides/css/css-in-js.mdx +0 -40
- package/zh/guides/css/css-modules.mdx +0 -87
- package/zh/guides/css/less-sass.mdx +0 -17
- package/zh/guides/css/postcss.mdx +0 -80
@@ -1,48 +1,48 @@
|
|
1
1
|
---
|
2
2
|
sidebar_position: 1
|
3
|
-
title:
|
3
|
+
title: Quick Start
|
4
4
|
---
|
5
|
-
#
|
5
|
+
# Quick Start
|
6
6
|
|
7
7
|
import ReduckMigration from "@site-docs/components/reduck-migration"
|
8
8
|
|
9
9
|
<ReduckMigration />
|
10
10
|
|
11
|
-
[Reduck](https://github.com/
|
11
|
+
[Reduck](https://github.com/web-infra-dev/reduck) is a state management library developed by the Modern.js team that follows the MVC pattern. Its underlying state storage is based on [Redux](https://redux.js.org/) implementation, while providing a higher level of abstraction and full compatibility with the Redux ecosystem.
|
12
12
|
|
13
|
-
Reduck
|
13
|
+
The goal of Reduck is to organize the development structure of React applications in the MVC pattern, maintain business logic in the Model layer, decoupling business logic from UI, making it easier for developers to focus on business logic, and reducing duplicated work (boilerplate code) through higher level of abstraction.
|
14
14
|
|
15
|
-
|
15
|
+
In the MVC pattern, Reduck plays the role of M(Model), React UI Component corresponds to V(View), and gets the Model from Reduck and modifies the Model's React Container Component, which corresponds to C(View Controller/Container).
|
16
16
|
|
17
|
-
Modern.js
|
17
|
+
The state management solution of Modern.js is implemented through built-in Reduck. Using Reduck in Modern.js not only eliminates the manual integration process, but also allows all Reduck APIs to be imported and used directly from the Modern.js Runtime package, providing a better consistency experience.
|
18
18
|
|
19
19
|
:::info
|
20
|
-
1. Modern.js
|
21
|
-
2. Reduck
|
20
|
+
1. To use Reduck APIs in Modern.js, you need to set [runtime.state](/configure/app/runtime/state) to enable the state management plugin.
|
21
|
+
2. Reduck can also be used separately as a state management library [outside of Modern.js](/guides/topic-detail/model/use-out-of-modernjs).
|
22
22
|
|
23
23
|
:::
|
24
24
|
|
25
|
-
##
|
25
|
+
## Core Concepts
|
26
26
|
|
27
|
-
|
27
|
+
There are only four core concepts in Reduck: Model, State, Actions, and Effects.
|
28
28
|
|
29
|
-
Model:
|
29
|
+
Model: Encapsulates the logic and required state of an independent module, consisting of State, Actions, and Effects.
|
30
30
|
|
31
|
-
State: Model
|
31
|
+
State: The state stored in the Model.
|
32
32
|
|
33
|
-
Actions:
|
33
|
+
Actions: Pure functions used to modify State, functions must be **Synchronous**.
|
34
34
|
|
35
|
-
Effects:
|
35
|
+
Effects: Functions with side effects used to modify State, functions can be **Asynchronous**. Effects can call their own Actions and Effects or those of other Models.
|
36
36
|
|
37
|
-
Reduck
|
37
|
+
The Reduck data flow is shown in the following figure:
|
38
38
|
|
39
|
-

|
40
40
|
|
41
|
-
##
|
41
|
+
## Basic Usage
|
42
42
|
|
43
|
-
|
43
|
+
Next, let's take a simple **Counter** application as an example to demonstrate the basic usage of Reduck.
|
44
44
|
|
45
|
-
|
45
|
+
First, we define a Model named `count`:
|
46
46
|
|
47
47
|
```js
|
48
48
|
import { model } from '@modern-js/runtime/model';
|
@@ -56,9 +56,9 @@ const countModel = model('count').define({
|
|
56
56
|
export default countModel;
|
57
57
|
```
|
58
58
|
|
59
|
-
|
59
|
+
We use the API `model` to create `countModel`, which currently only contains the state that stores the counter `value`, that is, value in the code.
|
60
60
|
|
61
|
-
|
61
|
+
We define an action to increase the counter by 1:
|
62
62
|
|
63
63
|
```js
|
64
64
|
import { model } from '@modern-js/runtime/model';
|
@@ -77,11 +77,11 @@ const countModel = model('count').define({
|
|
77
77
|
export default countModel;
|
78
78
|
```
|
79
79
|
|
80
|
-
|
80
|
+
In the `add` action, we can directly modify the value of the state and perform the increment operation without treating the state as an immutable object. This is because Reduck integrates [immer](https://github.com/immerjs/immer), which can directly modify the original state object.
|
81
81
|
|
82
|
-
|
82
|
+
Next, we will demonstrate how to use the Model in a component.
|
83
83
|
|
84
|
-
|
84
|
+
Create a new component called `Counter`, and use the `countModel` via the `useModel` API inside the component:
|
85
85
|
|
86
86
|
```js
|
87
87
|
import { useModel } from '@modern-js/runtime/model';
|
@@ -99,15 +99,16 @@ function Counter() {
|
|
99
99
|
}
|
100
100
|
```
|
101
101
|
|
102
|
-
`useModel`
|
102
|
+
`useModel` gets the `state` and `actions` of `countModel`. The component displays the current value of the counter, and clicking the `add` button increments the counter by 1.
|
103
103
|
|
104
104
|
:::info
|
105
|
-
|
105
|
+
Due to the simplicity of the example used here, the layering of the MVC pattern is not strictly followed. The `Counter` component acts as both the V and C layers.
|
106
106
|
|
107
107
|
:::
|
108
108
|
|
109
|
-
|
109
|
+
The final demonstration effect is as follows:
|
110
110
|
|
111
111
|

|
112
112
|
|
113
|
-
|
113
|
+
That completes a simple counter application. You can view the complete example code for this section [here](https://github.com/web-infra-dev/modern-js-examples/tree/main/series/tutorials/runtime-api/model/counter-model).
|
114
|
+
|
@@ -1,17 +1,18 @@
|
|
1
1
|
---
|
2
2
|
sidebar_position: 11
|
3
|
-
title:
|
3
|
+
title: Ecosystem Integration
|
4
4
|
---
|
5
|
-
# Redux
|
5
|
+
# Redux Ecosystem Integration
|
6
6
|
|
7
|
-
Reduck
|
7
|
+
Reduck is based on Redux, so you can use libraries from the [Redux ecosystem](https://redux.js.org/introduction/ecosystem) to enhance its functionality. APIs like [`Provider`](/apis/app/runtime/model/Provider), [`createApp`](/apis/app/runtime/model/create-app), and [`createStore`](/apis/app/runtime/model/create-store) allow you to configure the use of [middlewares](https://redux.js.org/understanding/thinking-in-redux/glossary#middleware) and [store enhancers](https://redux.js.org/understanding/thinking-in-redux/glossary#store-enhancer); and using [`createStore`](/apis/app/runtime/model/create-store), you can take complete control over the process of creating the store.
|
8
|
+
|
9
|
+
For example, if we want to use the middleware [`redux-logger`](https://github.com/LogRocket/redux-logger), the example code is as follows:
|
8
10
|
|
9
|
-
例如,我们希望使用中间件 [`redux-logger`](https://github.com/LogRocket/redux-logger),示例代码如下:
|
10
11
|
|
11
12
|
```ts
|
12
13
|
ReactDOM.render(
|
13
14
|
<Provider config={{ middlewares: [logger] }}>
|
14
|
-
//
|
15
|
+
// Set middleware through the config parameter of Provider
|
15
16
|
<App />
|
16
17
|
</Provider>,
|
17
18
|
document.getElementById('root'),
|
@@ -19,6 +20,5 @@ ReactDOM.render(
|
|
19
20
|
```
|
20
21
|
|
21
22
|
:::caution
|
22
|
-
Reduck
|
23
|
-
|
23
|
+
Reduck is built on top of the lower-level Redux API, and abstracts away some of the underlying concepts of Redux, such as Reducers. Reduck allows models to be dynamically mounted, whereas Redux mounts all the necessary state at Store creation time. Due to these implementation differences, some libraries from the Redux ecosystem cannot be used directly in Reduck.
|
24
24
|
:::
|
@@ -1,21 +1,21 @@
|
|
1
1
|
---
|
2
2
|
sidebar_position: 9
|
3
|
-
title:
|
3
|
+
title: Test Model
|
4
4
|
---
|
5
|
-
#
|
5
|
+
# Test Model
|
6
6
|
|
7
|
-
|
7
|
+
Testing is crucial for the stability of code. Here's an example using the `countModel` from [Quick Start](/guides/topic-detail/model/quick-start) to demonstrate how to perform unit testing on a Model in Modern.js.
|
8
8
|
|
9
|
-
|
9
|
+
To use the testing feature, you need to first enable it. In the project root directory, execute `pnpm run new` and make the following selection:
|
10
10
|
|
11
11
|
```bash
|
12
|
-
?
|
13
|
-
?
|
12
|
+
? Please select the operation you want to perform: Enable optional features
|
13
|
+
? Enable optional features Enable "Unit Testing / Integration Testing" feature
|
14
14
|
```
|
15
15
|
|
16
|
-
|
16
|
+
This will enable testing feature support.
|
17
17
|
|
18
|
-
|
18
|
+
Create a new file called `count.test.ts` with the following code:
|
19
19
|
|
20
20
|
```ts
|
21
21
|
import { createStore } from '@modern-js/runtime/testing';
|
@@ -36,10 +36,10 @@ describe('test model', () => {
|
|
36
36
|
```
|
37
37
|
|
38
38
|
:::info
|
39
|
-
|
39
|
+
The [`createStore`](/apis/app/runtime/model/create-store) used here is imported from `@modern-js/runtime/testing`, which internally uses the configuration of [`runtime.state`](/configure/app/runtime/state) to create a `store`.
|
40
40
|
|
41
41
|
:::
|
42
42
|
|
43
|
-
|
43
|
+
In the test case, we create a new `store` to mount `countModel`, use `store.use` to get the State and Actions of `countModel`. Then, we call the `add` Action to update the state and assert the updated state value.
|
44
44
|
|
45
|
-
|
45
|
+
Execute the `pnpm run test` command to trigger the execution of the test case.
|
@@ -1,16 +1,16 @@
|
|
1
1
|
---
|
2
2
|
sidebar_position: 10
|
3
|
-
title: TS
|
3
|
+
title: TS Best Practices
|
4
4
|
---
|
5
|
-
# TS
|
5
|
+
# TS Best Practices
|
6
6
|
|
7
|
-
Reduck
|
7
|
+
Reduck provides excellent support for TypeScript, and in most cases, you can get API type prompts directly without any extra work. In this section, we will provide additional information on other usage scenarios.
|
8
8
|
|
9
|
-
##
|
9
|
+
## Defining the State Type of the Model
|
10
10
|
|
11
|
-
|
11
|
+
Declaring type information for the State of a Model is a best practice when using Reduck in TypeScript.
|
12
12
|
|
13
|
-
```ts
|
13
|
+
```ts
|
14
14
|
interface State {
|
15
15
|
data: string;
|
16
16
|
}
|
@@ -30,13 +30,14 @@ export const foo = model<State>('foo').define({
|
|
30
30
|
});
|
31
31
|
```
|
32
32
|
|
33
|
-
|
33
|
+
When you declare type information for the State of a Model, the `computed` and `actions` of the Model can get the correct type information. In fact, even if we don't define State type information in the example code above, the type information of State can be automatically inferred based on the initial value information of `state`. However, we still recommend that you declare the type information of State when defining a Model because the type information of State inferred based on the initial value information of `state` may be incomplete (missing fields or field type information), and the type information of State cannot be automatically inferred based on the initial value information of `state` when using [Function Type](/apis/app/runtime/model/model_#函数类型) to define the Model.
|
34
34
|
|
35
|
-
##
|
35
|
+
## Dependent types of Derived State
|
36
36
|
|
37
|
-
|
37
|
+
When the derived state of a Model depends on other Models, you need to manually specify the State of the other Models.
|
38
38
|
|
39
|
-
|
39
|
+
|
40
|
+
```ts
|
40
41
|
interface State {
|
41
42
|
data: string;
|
42
43
|
}
|
@@ -52,14 +53,14 @@ export const bar = model<State>('bar').define({
|
|
52
53
|
});
|
53
54
|
```
|
54
55
|
|
55
|
-
##
|
56
|
+
## Hooks for Getting Model Type Information
|
56
57
|
|
57
|
-
Reduck
|
58
|
+
Reduck provides a set of utility types for getting Model type information:
|
58
59
|
|
59
|
-
- `GetModelState
|
60
|
-
- `GetModelActions
|
60
|
+
- `GetModelState`: Get the type information of the State (including derived state) of the Model.
|
61
|
+
- `GetModelActions`: Get the type information of the Actions (including Effects functions) of the Model.
|
61
62
|
|
62
|
-
```ts
|
63
|
+
```ts
|
63
64
|
export const foo = model<State2>('foo').define({
|
64
65
|
// 省略
|
65
66
|
});
|
@@ -1,18 +1,18 @@
|
|
1
1
|
---
|
2
2
|
sidebar_position: 3
|
3
|
-
title:
|
3
|
+
title: Use Models
|
4
4
|
---
|
5
|
-
#
|
5
|
+
# Use Models
|
6
6
|
|
7
|
-
##
|
7
|
+
## Using Models in Components
|
8
8
|
|
9
|
-
###
|
9
|
+
### Using as Global State
|
10
10
|
|
11
|
-
|
11
|
+
`useModel` can be used to obtain the State, Actions, and other information of the Model. When the State of the Model is modified by Actions, any other components that use the Model will automatically re-render.
|
12
12
|
|
13
|
-
|
13
|
+
In the counter example in [Quick Start](/guides/topic-detail/model/quick-start), we have demonstrated the use of `useModel` and will not repeat it here.
|
14
14
|
|
15
|
-
`useModel`
|
15
|
+
`useModel` supports passing multiple Models, and the State and Actions of multiple Models will be merged and returned as the result. For example:
|
16
16
|
|
17
17
|
```ts
|
18
18
|
const fooModel = model('foo').define({
|
@@ -42,7 +42,7 @@ const [state, actions] = useModel([fooModel, barModel]);
|
|
42
42
|
const [state, actions] = useModel(fooModel, barModel);
|
43
43
|
```
|
44
44
|
|
45
|
-
`state`
|
45
|
+
`state` and `actions` value are:
|
46
46
|
|
47
47
|
```ts
|
48
48
|
state = {
|
@@ -60,7 +60,7 @@ actions = {
|
|
60
60
|
};
|
61
61
|
```
|
62
62
|
|
63
|
-
`useModel`
|
63
|
+
`useModel` also supports selector operations on State and Actions to filter or rename State and Actions. For example:
|
64
64
|
|
65
65
|
```ts
|
66
66
|
const fooModel = model('foo').define({
|
@@ -95,9 +95,9 @@ const [state, actions] = useModel(
|
|
95
95
|
);
|
96
96
|
```
|
97
97
|
|
98
|
-
|
98
|
+
We use `stateSelector` to rename the states with the same name in `fooModel` and `barModel`. We use `actionsSelector` to filter out the Actions of `barModel`.
|
99
99
|
|
100
|
-
|
100
|
+
If only `actionsSelector` needs to be set, you can set `stateSelector` to `undefined` as a placeholder. For example:
|
101
101
|
|
102
102
|
```ts
|
103
103
|
const [state, actions] = useModel(
|
@@ -107,22 +107,21 @@ const [state, actions] = useModel(
|
|
107
107
|
);
|
108
108
|
```
|
109
109
|
|
110
|
-
###
|
110
|
+
### Using as Static State
|
111
111
|
|
112
|
-
|
112
|
+
`useStaticModel` can be used to obtain the Model and use the state of the Model as a static state. This ensures that the State accessed by the component is always the latest value, but the change of the Model's State does not cause the current component to re-render.
|
113
113
|
|
114
114
|
:::info
|
115
|
-
`useStaticModel`
|
116
|
-
|
115
|
+
The usage of `useStaticModel` is exactly the same as `useModel`.
|
117
116
|
:::
|
118
117
|
|
119
|
-
|
118
|
+
Consider the following scenario: there is an Input component responsible for user input, and another Search component responsible for executing a search operation after the user input information is entered and the search button is clicked. We do not want the state changes during the user input process to cause Search to re-render. In this case, `useStaticModel` can be used:
|
120
119
|
|
121
120
|
```ts
|
122
121
|
import { useStaticModel } from '@modern-js/runtime/model';
|
123
122
|
|
124
123
|
function Search() {
|
125
|
-
//
|
124
|
+
// should not be destructured
|
126
125
|
const [state] = useStaticModel(searchModel);
|
127
126
|
|
128
127
|
return (
|
@@ -140,41 +139,40 @@ function Search() {
|
|
140
139
|
}
|
141
140
|
```
|
142
141
|
|
143
|
-
:::warning
|
144
|
-
|
142
|
+
:::warning Caution
|
143
|
+
Do not destructure the `state` returned by `useStaticModel`. For example, changing it to the following code:
|
145
144
|
`const [{input}] = useStaticModel(searchModel);`
|
146
|
-
|
145
|
+
will always get the initial value of Input.
|
147
146
|
|
148
147
|
:::
|
149
148
|
|
150
|
-
`useStaticModel`
|
149
|
+
`useStaticModel` is also suitable for use with animation libraries such as [react-three-fiber](https://github.com/pmndrs/react-three-fiber), because binding fast-changing states in animation component UI can easily cause [performance issues](https:/docs.pmnd.rs/react-three-fiber/advanced/pitfalls#never-bind-fast-state-reactive). In this case, you can choose to use `useStaticModel`, which only subscribes to the State but does not cause the view component to re-render. Here is a simplified example:
|
151
150
|
|
152
151
|
```ts
|
153
152
|
function ThreeComponent() {
|
154
153
|
const [state, actions] = useStaticModel(modelA);
|
155
154
|
|
156
155
|
useFrame(() => {
|
157
|
-
state.value;
|
156
|
+
state.value;
|
158
157
|
actions.setValue(1);
|
159
|
-
state.value;
|
158
|
+
state.value;
|
160
159
|
});
|
161
160
|
}
|
162
161
|
```
|
163
162
|
|
164
|
-
|
163
|
+
Using React's refs can also achieve similar effects. In fact, `useStaticModel` also uses refs internally. However, using `useStaticModel` directly helps decouple the state management logic from the component and converge it into the Model layer.
|
165
164
|
|
166
|
-
|
165
|
+
The complete sample code can be found [here](https://github.com/web-infra-dev/modern-js-examples/tree/main/series/tutorials/runtime-api/model/static-model).
|
167
166
|
|
168
|
-
###
|
167
|
+
### Using as Local State
|
169
168
|
|
170
|
-
|
169
|
+
`useLocalModel` can be used to obtain the Model and use the state of the Model as local state. At this time, the change of the Model State only causes the current component to re-render, but does not cause other components that use the Model to re-render. The effect is similar to managing state through `useState` in React, but it can decouple the state management logic from the component and converge it into the Model layer.
|
171
170
|
|
172
171
|
:::info
|
173
|
-
`useLocalModel`
|
174
|
-
|
172
|
+
The usage of `useLocalModel` is exactly the same as `useModel`.
|
175
173
|
:::
|
176
174
|
|
177
|
-
|
175
|
+
For example, we modify the code of the counter application and add a counter component `LocalCounter` with local state:
|
178
176
|
|
179
177
|
```ts
|
180
178
|
import { useLocalModel } from '@modern-js/runtime/model';
|
@@ -191,20 +189,20 @@ function LocalCounter() {
|
|
191
189
|
}
|
192
190
|
```
|
193
191
|
|
194
|
-
|
192
|
+
Click the `add` button of `Counter` and `LocalCounter` respectively, and the states of the two do not affect each other:
|
195
193
|
|
196
194
|

|
197
195
|
|
198
|
-
|
196
|
+
The complete sample code can be found [here](https://github.com/web-infra-dev/modern-js-examples/tree/main/series/tutorials/runtime-api/model/local-model).
|
199
197
|
|
200
|
-
##
|
198
|
+
## Using outside of components
|
201
199
|
|
202
|
-
|
200
|
+
In actual business scenarios, sometimes we need to use Model outside of React components, such as accessing State and executing Actions in utility functions. At this time, we need to use the Store. The Store is a low-level concept that users generally cannot touch. It is responsible for storing and managing the entire application's state. Reduck's Store is based on Redux's Store implementation and adds Reduck-specific APIs, such as `use`.
|
203
201
|
|
204
|
-
|
202
|
+
First, call `useStore` in the component to obtain the `store` object used by the current application and mount it to a variable outside the component:
|
205
203
|
|
206
204
|
```ts
|
207
|
-
let store; //
|
205
|
+
let store; // Reference to `store` object outside of the component
|
208
206
|
function setStore(s) {
|
209
207
|
store = s;
|
210
208
|
}
|
@@ -215,7 +213,7 @@ function getStore() {
|
|
215
213
|
function Counter() {
|
216
214
|
const [state] = useModel(countModel);
|
217
215
|
const store = useStore();
|
218
|
-
//
|
216
|
+
// Avoid unnecessary duplicate settings through `useMemo`
|
219
217
|
useMemo(() => {
|
220
218
|
setStore(store);
|
221
219
|
}, [store]);
|
@@ -228,8 +226,7 @@ function Counter() {
|
|
228
226
|
}
|
229
227
|
```
|
230
228
|
|
231
|
-
|
232
|
-
执行自增操作:
|
229
|
+
You can obtain the Model object through `store.use`, and the usage of `store.use` is the same as `useModel`. Taking the counter application as an example, we perform an increment operation on the counter value every 1 second outside the component tree:
|
233
230
|
|
234
231
|
```ts
|
235
232
|
setInterval(() => {
|
@@ -239,14 +236,12 @@ setInterval(() => {
|
|
239
236
|
}, 1000);
|
240
237
|
```
|
241
238
|
|
242
|
-
|
239
|
+
The complete sample code can be found [here](https://github.com/web-infra-dev/modern-js-examples/tree/main/series/tutorials/runtime-api/model/counter-model-outof-react).
|
243
240
|
|
244
241
|
:::info
|
245
|
-
|
246
|
-
|
242
|
+
If the Store object is manually created through [`createStore`](/apis/app/runtime/model/create-store), there is no need to obtain it through `useStore` in the component, and it can be used directly.
|
247
243
|
:::
|
248
244
|
|
249
|
-
:::info
|
250
|
-
|
251
|
-
|
245
|
+
:::info Additional Information
|
246
|
+
For detailed API definitions related to this section, please refer to [here](/apis/app/runtime/model/model_).
|
252
247
|
:::
|
@@ -1,47 +1,47 @@
|
|
1
1
|
---
|
2
2
|
sidebar_position: 12
|
3
|
-
title:
|
3
|
+
title: Using Reduck Separately
|
4
4
|
---
|
5
|
-
#
|
5
|
+
# Using Reduck Separately
|
6
6
|
|
7
|
-
|
7
|
+
When integrating Reduck separately from Modern.js, the following modifications need to be made:
|
8
8
|
|
9
|
-
1.
|
9
|
+
1. Install the Reduck related packages
|
10
10
|
|
11
|
-
|
11
|
+
Install the Reduck package in the project: `@modern-js-reduck/react`.
|
12
12
|
|
13
|
-
2. API
|
13
|
+
2. Import package names for API
|
14
14
|
|
15
|
-
|
15
|
+
When used in Modern.js, the package name for exporting Reduck API is: `@modern-js/runtime/model`. When using Reduck separately, the package name for exporting is: `@modern-js-reduck/react`.
|
16
16
|
|
17
|
-
3.
|
17
|
+
3. Wrap the `Provider` component
|
18
18
|
|
19
|
-
Modern.js
|
19
|
+
Modern.js automatically wraps the [`Provider`](/apis/app/runtime/model/Provider) component used to inject the Reduck global Store on the entry component of the application. When using Reduck separately, this needs to be done manually.
|
20
20
|
|
21
|
-
|
21
|
+
Example:
|
22
22
|
|
23
23
|
```tsx
|
24
|
-
//
|
24
|
+
// Root Component
|
25
25
|
const Root = () => {
|
26
26
|
return (
|
27
27
|
<Provider>
|
28
|
-
{/*
|
28
|
+
{/* Entry App */}
|
29
29
|
<App />
|
30
30
|
</Provider>
|
31
31
|
);
|
32
32
|
};
|
33
33
|
```
|
34
34
|
|
35
|
-
4.
|
35
|
+
4. Feature configuration
|
36
36
|
|
37
|
-
|
37
|
+
When used in Modern.js, Reduck features can be configured through [`runtime.state`](/configure/app/runtime/state). When used separately, configuration needs to be done through the `config` or `store` parameter of [`Provider`](/apis/app/runtime/model/Provider).
|
38
38
|
|
39
|
-
|
39
|
+
Example:
|
40
40
|
|
41
41
|
```tsx
|
42
42
|
const Root = () => {
|
43
43
|
return (
|
44
|
-
{/*
|
44
|
+
{/* Close Redux DevTools */}
|
45
45
|
<Provider config={{ devTools: false }}>
|
46
46
|
<App />
|
47
47
|
</Provider>
|
@@ -58,6 +58,6 @@ In the above example the parameter `--option` is passed to `modern command`. If
|
|
58
58
|
|
59
59
|
Summary:
|
60
60
|
|
61
|
-
**
|
61
|
+
**When using pnpm v7, if you pass arguments to pnpm, you need to put the arguments before the command**
|
62
62
|
|
63
|
-
**
|
63
|
+
**When using pnpm v6, if the parameter passed to pnpm, you do not need to add `--`; if the parameter passed is for script use, you need to add `--` character string**.
|
@@ -293,7 +293,7 @@ A Utility Class named `custom-text-gray` is implemented in `src/routes/styles/ut
|
|
293
293
|
```
|
294
294
|
|
295
295
|
:::info note
|
296
|
-
Modern.js integrates with [PostCSS](/guides/css
|
296
|
+
Modern.js integrates with [PostCSS](/guides/basic-features/css) and supports modern CSS syntax features such as [custom properties](https://developer.mozilla.org/en-US/docs/Web/CSS/Using_CSS_custom_properties).
|
297
297
|
|
298
298
|
:::
|
299
299
|
|
package/package.json
CHANGED
@@ -2,8 +2,8 @@
|
|
2
2
|
"name": "@modern-js/main-doc",
|
3
3
|
"description": "Shared documentation of modern.js framework",
|
4
4
|
"homepage": "https://modernjs.dev",
|
5
|
-
"bugs": "https://github.com/
|
6
|
-
"repository": "
|
5
|
+
"bugs": "https://github.com/web-infra-dev/modern.js/issues",
|
6
|
+
"repository": "web-infra-dev/modern.js",
|
7
7
|
"license": "MIT",
|
8
8
|
"keywords": [
|
9
9
|
"react",
|
@@ -11,13 +11,13 @@
|
|
11
11
|
"modern",
|
12
12
|
"modern.js"
|
13
13
|
],
|
14
|
-
"version": "2.
|
14
|
+
"version": "2.8.0",
|
15
15
|
"publishConfig": {
|
16
16
|
"registry": "https://registry.npmjs.org/",
|
17
17
|
"access": "public"
|
18
18
|
},
|
19
19
|
"peerDependencies": {
|
20
|
-
"@modern-js/builder-doc": "^2.
|
20
|
+
"@modern-js/builder-doc": "^2.8.0"
|
21
21
|
},
|
22
22
|
"devDependencies": {
|
23
23
|
"ts-node": "^10",
|
@@ -25,7 +25,7 @@
|
|
25
25
|
"fs-extra": "^10",
|
26
26
|
"@types/node": "^16",
|
27
27
|
"@types/fs-extra": "^9",
|
28
|
-
"@modern-js/builder-doc": "2.
|
28
|
+
"@modern-js/builder-doc": "2.8.0"
|
29
29
|
},
|
30
30
|
"scripts": {
|
31
31
|
"build": "npx ts-node ./scripts/sync.ts"
|
package/zh/apis/app/commands.mdx
CHANGED
@@ -20,6 +20,7 @@ Options:
|
|
20
20
|
-c --config <config> 指定配置文件路径,可以为相对路径或绝对路径
|
21
21
|
-h, --help 显示命令帮助
|
22
22
|
--analyze 分析构建产物体积,查看各个模块打包后的大小
|
23
|
+
--web-only 仅启动 Web 服务
|
23
24
|
--api-only 仅启动 API 接口服务
|
24
25
|
```
|
25
26
|
|
@@ -161,6 +162,7 @@ Usage: modern serve [options]
|
|
161
162
|
Options:
|
162
163
|
-c --config <config> 指定配置文件路径,可以为相对路径或绝对路径
|
163
164
|
-h, --help 显示命令帮助
|
165
|
+
--web-only 仅启动 Web 服务
|
164
166
|
--api-only 仅启动 API 接口服务
|
165
167
|
```
|
166
168
|
|