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
* chore: model validate show name
* fix: fix can't get correct state (#95)
* fix: fix can't get correct state
* chore: update version
* fix: fix by the comment
* docs: readonly && get the latest state
Co-authored-by: Hengchang Lu <44047106+luhc228@users.noreply.github.com>
Copy file name to clipboardExpand all lines: docs/recipes.md
+95-20Lines changed: 95 additions & 20 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -67,6 +67,81 @@ For example, the action A in Model A calls the action B in Model B and the actio
67
67
68
68
Be careful the possibility of endless loop problem will arise when methods from different models call each other.
69
69
70
+
## Readonly
71
+
72
+
In some scenarios, you may only want to call the method returned by the model to update the state instead of subscribing to the update of the model state. For example, the button component in the "Basic example", you do not consume the state of the model in the component, so you may not expect the change of the state of the model to trigger the re-rende of the component.
73
+
74
+
At this time, you can use the `useModelDispatchers` API, check following example:
75
+
76
+
```js
77
+
importstorefrom'@/store';
78
+
const { useModelDispatchers } = store;
79
+
functionButton() {
80
+
const [, dispatchers ] =useModel('counter'); // The update of model'state will be subscribed here
In some scenarios, you may want to get the latest state of the model.
98
+
99
+
### In Component
100
+
101
+
```js
102
+
importstorefrom'@/store';
103
+
104
+
functionLogger({ foo }) {
105
+
// case 1 use state only instead of subscribing to updates(a means of performance optimization)
106
+
functiondoSomeThing() {
107
+
constcounter=store.getModelState('counter');
108
+
alert(counter);
109
+
};
110
+
111
+
112
+
// case 2 get the latest state in the closure
113
+
constdoOhterThing=useCallback(
114
+
(payload) => {
115
+
constcounter=store.getModelState('counter');
116
+
alert(counter + foo);
117
+
},
118
+
[foo]
119
+
);
120
+
121
+
return (
122
+
<div>
123
+
<button onClick={doSomeThing}>click 1<button>
124
+
<button onClick={doOhterThing}>click 2<button>
125
+
</div>
126
+
);
127
+
}
128
+
```
129
+
130
+
### In Model
131
+
132
+
```js
133
+
importstorefrom'@/store';
134
+
135
+
constuser= {
136
+
effects:dispatch=> ({
137
+
asyncaddByAsync(payload, state) {
138
+
dispatch.todos.addTodo(payload); // Call methods of other models to update their state
139
+
consttodos=store.getModelState('todos'); // Get the latest state of the updated model
140
+
}
141
+
})
142
+
}
143
+
```
144
+
70
145
## effects' executing status
71
146
72
147
`icestore` has built-in support to access the executing status of effects. This enables users to have access to the isLoading and error executing status of effects without defining extra state, making the code more consise and clean.
@@ -161,26 +236,6 @@ You can use `withModelDispatchers` to call only model actions without listening
161
236
162
237
See [docs/api](./api.md) for more details.
163
238
164
-
## Directory organization
165
-
166
-
For most small and medium-sized projects, it is recommended to centrally manage all the project models in the global `src/models/` directory:
167
-
168
-
```bash
169
-
├── src/
170
-
│ ├── components/
171
-
│ │ └── NotFound/
172
-
│ ├── pages/
173
-
│ │ └── Home
174
-
│ ├── models/
175
-
│ │ ├── modelA.js
176
-
│ │ ├── modelB.js
177
-
│ │ ├── modelC.js
178
-
│ │ └── index.js
179
-
│ └── store.js
180
-
```
181
-
182
-
If the project is relatively large, or more likely to follow the page maintenance of the store,then you can declare a store instance in each page directory. However, in this case, cross page store calls should be avoided as much as possible.
183
-
184
239
## Immutable Description
185
240
186
241
### Don't destructure the state argument
@@ -234,6 +289,26 @@ const store = createStore(models, {
234
289
});
235
290
```
236
291
292
+
## Directory organization
293
+
294
+
For most small and medium-sized projects, it is recommended to centrally manage all the project models in the global `src/models/` directory:
295
+
296
+
```bash
297
+
├── src/
298
+
│ ├── components/
299
+
│ │ └── NotFound/
300
+
│ ├── pages/
301
+
│ │ └── Home
302
+
│ ├── models/
303
+
│ │ ├── modelA.js
304
+
│ │ ├── modelB.js
305
+
│ │ ├── modelC.js
306
+
│ │ └── index.js
307
+
│ └── store.js
308
+
```
309
+
310
+
If the project is relatively large, or more likely to follow the page maintenance of the store,then you can declare a store instance in each page directory. However, in this case, cross page store calls should be avoided as much as possible.
0 commit comments