Newer
Older
## Installation
One of the following:
- `npm i -S @commander-lol/redux-reducer`
- `yarn add @commander-lol/redux-reducer`
- `ied i -S @commander-lol/redux-reducer`
redux-reducer strips out the common boilerplate used to determine what actions to take when a reducer is run, as well as allowing
you to programmatically compose a reducer at runtime. It also handles the default case for you, saving potentially hours
of bug hunting when you overlook it.
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
## Usage
When you're writing reducers for Redux, you probably have something similar to the following:
```js
const initialState = {
dongles: {},
}
export default function myReducer(state = initialState, {type, ...action}) {
switch(type) {
case "ADD_DONGLE": {
let { dongles } = state
let { dongle } = action
dongles = {
...dongles,
[dongle.id]: dongle,
}
return {
...state,
dongles,
}
}
default:
return state
}
}
```
And that's a pattern that will appear in pretty much every one of your reducers. Instead, using the `redux-reducer` library
turns that into the following:
```js
const reducer = require('@commander-lol/redux-reducer')
const initialState = {
dongles: {},
}
export default reducer(initialState, {
ADD_DONGLE: ({ dongles, ...state }, { dongle }) => {
return {
...state,
dongles: {
...dongles,
[dongle.id] = dongle,
},
}
}
})
```
## API
The library exports a single function that returns a redux-compatible reducer function;
``` reducer(initial: Object, handlers: Map<string, Handler>) ```
Where initial is the `initial` router state and `handlers` is a string -> function map of action types to action reducers. Each handler is passed `state` and `action` as parameters, which correspond to the assigned reducer state and the current action (without the `type` property) and should return the new version of state after resolving the action. Simple as that.