Skip to content

Commit 664740d

Browse files
author
Péter Hauszknecht
committed
update readme
1 parent c537741 commit 664740d

1 file changed

Lines changed: 23 additions & 2 deletions

File tree

README.md

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,16 @@ const setX = x => reduceFoo(state => ({ ...state, x }));
8484

8585
## Middlewares
8686

87-
A repatch middleware takes the store instance and the previous reducer and returns a new reducer:
87+
A repatch middleware takes the `store` instance, a `next` function and the previous `reducer`. The middleware can provide a new reducer via the `next` function.
8888

8989
```javascript
90-
(Store, Reducer): Reducer
90+
Middleware: Store -> (Reducer -> Reducer) -> Reducer -> any
91+
```
92+
93+
where
94+
95+
```javascript
96+
Next: Reducer -> Reducer
9197
```
9298

9399
Use the `addMiddleware` method to chaining middlewares:
@@ -98,6 +104,21 @@ const store = new Store(initialState)
98104
.addMiddleware(mw2, mw3);
99105
```
100106

107+
## Middleware example
108+
109+
This simple logger middleware logs the current- and the next state:
110+
111+
```javascript
112+
const logger = store => next => reducer => {
113+
const state = store.getState()
114+
const nextState = reducer(state)
115+
console.log(state, nextState)
116+
return next(_ => nextState)
117+
}
118+
119+
const store = new Store(initialState).addMiddleware(logger)
120+
```
121+
101122
## Async actions
102123

103124
The `thunk` middleware is useful for handling async actions similar to [redux-thunk](https://www.npmjs.com/package/redux-thunk).

0 commit comments

Comments
 (0)