@@ -23,17 +23,28 @@ import { build } from 'encaps';
23
23
24
24
export const { actions , reducer } = build ()
25
25
.initState (() => ({counter: 10 }))
26
+ /**
27
+ * Functions to handle actions
28
+ * A reducer will consist of them
29
+ */
26
30
.handlers ({
27
31
increment : (state , action ) => ({... state, counter: state .counter + action .payload }),
28
32
decrement : (state , action ) => ({... state, counter: state .counter - action .payload }),
33
+ /**
34
+ * a string value is a shortcut for
35
+ * (state, action) => ({...state, counter: action.payload}),
36
+ */
37
+ setCounter: ' counter'
29
38
});
30
39
31
40
const initState = reducer (); // {counter: 10}
32
41
33
42
actions .increment (1 ); // {type: 'increment', payload: 1}
34
43
actions .decrement (2 ); // {type: 'decrement', payload: 2}
44
+ actions .setCounter (3 ); // {type: 'setCounter', payload: 3}
35
45
36
46
reducer (initState, actions .decrement (2 )); // {counter: 8}
47
+ reducer (initState, actions .setCounter (3 )); // {counter: 3}
37
48
```
38
49
39
50
### Reducer extension
@@ -268,8 +279,11 @@ interface Builder {
268
279
* @returns new Builder
269
280
*/
270
281
handlers(
271
- /** map of action handlers */
272
- handlers : {[K : string ]: (state , action : Action ) => object }
282
+ /**
283
+ * Map of action handlers
284
+ * You can set a model field name instead of a function to create a function which changes this field of the model
285
+ */
286
+ handlers : {[K : string ]: ((state , action : Action ) => object ) | string }
273
287
): Builder ;
274
288
275
289
/**
0 commit comments