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: README.md
+23-2Lines changed: 23 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -84,10 +84,16 @@ const setX = x => reduceFoo(state => ({ ...state, x }));
84
84
85
85
## Middlewares
86
86
87
-
A repatch middleware takes the store instanceand 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.
88
88
89
89
```javascript
90
-
(Store, Reducer): Reducer
90
+
Middleware: Store -> (Reducer -> Reducer) -> Reducer -> any
91
+
```
92
+
93
+
where
94
+
95
+
```javascript
96
+
Next: Reducer -> Reducer
91
97
```
92
98
93
99
Use the `addMiddleware` method to chaining middlewares:
@@ -98,6 +104,21 @@ const store = new Store(initialState)
98
104
.addMiddleware(mw2, mw3);
99
105
```
100
106
107
+
## Middleware example
108
+
109
+
This simple logger middleware logs the current- and the next state:
Copy file name to clipboardExpand all lines: docs/store.md
+14-16Lines changed: 14 additions & 16 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -46,11 +46,11 @@ Dispatches a reducer.
46
46
47
47
#### Arguments
48
48
49
-
1)**reducer** (*ReducerFunction: State -> State*): That reducer will reduce the state of the store. This takes the current state and returns the next state. The reducer will be run synchronously after applying the middlewares - if they are given.
49
+
1)**reducer** (*ReducerFunction: State -> State*): That reducer will reduce the state of the store. This takes the current state and returns the next state.
50
50
51
51
#### Returns
52
52
53
-
(*ReducerFunction*): The final reducer that is made by applying the middlewares - if they are given. If the store does not have middlewares, the `dispatch` method returns the same reducer that was taken as argument.
53
+
(*ReducerFunction*): The final reducer that is made by applying the middlewares - if they are given. If the store does enhanced with middlewares, the `dispatch` method returns the same reducer that was taken as argument.
1) Middlewares can modify the given reducer, so that is not guaranteed that the `dispatch` returns the original reducer that was taken as argument.
70
-
2) The `dispatch` only runs the reducer, when the final reducer (after applying middlewares) is still a function. If the final reducer is a function, then the dispatch modifies the state by its returned value. This behaviour is strongly used by the [thunk](thunk.md) middleware, that returns the returned value of the delegate.
69
+
Middlewares can modify the given reducer, so that is not guaranteed that the `dispatch` returns the original reducer that was taken as argument.
71
70
72
71
### `subscribe(listener)`
73
72
@@ -99,33 +98,32 @@ store.dispatch(increment) // listener won't be fired
99
98
100
99
### `addMiddleware(...middlewares)`
101
100
102
-
Adds middleware(s) to the store.
101
+
Enhances the store with the given middleware(s).
103
102
104
-
Middlewares will be run at dispatching before the store applies the new state of the reducer. The added middlewares are composed by order of addition.
103
+
Middlewares will be run at dispatching before the store applies the new state of the reducer. The added middlewares are composed by order of addition, so the last added middleware will run first.
105
104
106
105
#### Arguments
107
106
108
-
1)**...middlewares** (*MiddlewareFunction: (Store, Reducer) -> Reducer*): Middlewares as variadic arguments. Middleware functions take the `Store` instanceand the dispatched reducer and return a new reducer.
107
+
1)**...middlewares** (*MiddlewareFunction: Store -> Next -> Reducer -> any*): Middlewares as variadic arguments. Middleware functions take the `store` instance, a `next` function and the previous `reducer`. The middleware can provide a new reducer via the `next` function.
109
108
110
109
#### Returns
111
110
112
-
(*Store: this*): Returns the same`Store` instance for chaining.
111
+
(*Store: this*): Returns the enhanced`Store` instance for chaining.
0 commit comments