Skip to content

Commit 103b146

Browse files
authored
Merge pull request #98 from byte-fe/develop
docs(middlewares): add: how to customize model's middlewares
2 parents ac9bd1f + 70183bb commit 103b146

File tree

2 files changed

+36
-8
lines changed

2 files changed

+36
-8
lines changed

README.md

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ npm install react-model
8484
- [How can I make persist models](#how-can-i-make-persist-models)
8585
- [How can I deal with local state](#how-can-i-deal-with-local-state)
8686
- [actions throw error from immer.module.js](#actions-throw-error-from-immer.module.js)
87+
- [How can I customize each model's middlewares?](#how-can-i-customize-each-model's-middlewares)
8788

8889
## Core Concept
8990

@@ -850,4 +851,36 @@ actions: {
850851
}
851852
```
852853
853-
[⇧ back to top](#table-of-contents)
854+
[⇧ back to top](#table-of-contents)
855+
856+
### How can I customize each model's middlewares?
857+
858+
If you are using NextModel, you can customize each model's middlewares.
859+
860+
```typescript
861+
import { actionMiddlewares, Model } from 'react-model'
862+
import { delayMiddleware } from './middlewares'
863+
864+
const nextCounterModel: NextModelType<CounterState, NextCounterActionParams> = {
865+
actions: {
866+
add: num => {
867+
return state => {
868+
state.count += num
869+
}
870+
},
871+
increment: async (num, { actions }) => {
872+
actions.add(num)
873+
await timeout(300, {})
874+
}
875+
},
876+
// You can define the custom middlewares here
877+
middlewares: [delayMiddleware, ...actionMiddlewares],
878+
state: {
879+
count: 0
880+
}
881+
}
882+
883+
export default Model(nextCounterModel)
884+
```
885+
886+
[⇧ back to top](#table-of-contents)

package.json

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-model",
3-
"version": "2.7.1-alpha.2",
3+
"version": "2.7.1",
44
"description": "The State management library for React",
55
"main": "./dist/react-model.js",
66
"umd:main": "./dist/react-model.umd.js",
@@ -13,12 +13,7 @@
1313
"test": "jest --silent",
1414
"test:coverage": "jest --collect-coverage --silent"
1515
},
16-
"keywords": [
17-
"react",
18-
"model",
19-
"state-management",
20-
"react-hooks"
21-
],
16+
"keywords": ["react", "model", "state-management", "react-hooks"],
2217
"author": "ArrayZoneYour <hustliyidong@gmail.com>",
2318
"license": "MIT",
2419
"dependencies": {

0 commit comments

Comments
 (0)