Skip to content

Commit 626bf59

Browse files
seniorquicotimche
authored andcommitted
Add support for default or initial state to handleAction. (#55)
1 parent 948cbf2 commit 626bf59

File tree

3 files changed

+23
-3
lines changed

3 files changed

+23
-3
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ createAction('ADD_TODO')('Use Redux');
5757

5858
`metaCreator` is an optional function that creates metadata for the payload. It receives the same arguments as the payload creator, but its result becomes the meta field of the resulting action. If `metaCreator` is undefined or not a function, the meta field is omitted.
5959

60-
### `handleAction(type, reducer | reducerMap)`
60+
### `handleAction(type, reducer | reducerMap, ?defaultState)`
6161

6262
Wraps a reducer so that it only handles Flux Standard Actions of a certain type.
6363

@@ -72,6 +72,8 @@ handleAction('FETCH_DATA', {
7272
});
7373
```
7474

75+
The optional third parameter specifies a default or initial state, which is used when `undefined` is passed to the reducer.
76+
7577
### `handleActions(reducerMap, ?defaultState)`
7678

7779
Creates multiple reducers using `handleAction()` and combines them into a single reducer that handles multiple actions. Accepts a map where the keys are passed as the first parameter to `handleAction()` (the action type), and the values are passed as the second parameter (either a reducer or reducer map).

src/__tests__/handleAction-test.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,14 @@ describe('handleAction()', () => {
1111
expect(reducer(prevState, { type })).to.equal(prevState);
1212
});
1313

14+
it('returns default state if type does not match', () => {
15+
const reducer = handleAction('NOTTYPE', () => null, { counter: 7 });
16+
expect(reducer(undefined, { type }))
17+
.to.deep.equal({
18+
counter: 7
19+
});
20+
});
21+
1422
it('accepts single function as handler', () => {
1523
const reducer = handleAction(type, (state, action) => ({
1624
counter: state.counter + action.payload
@@ -20,6 +28,16 @@ describe('handleAction()', () => {
2028
counter: 10
2129
});
2230
});
31+
32+
it('accepts single function as handler and a default state', () => {
33+
const reducer = handleAction(type, (state, action) => ({
34+
counter: state.counter + action.payload
35+
}), { counter: 3 });
36+
expect(reducer(undefined, { type, payload: 7 }))
37+
.to.deep.equal({
38+
counter: 10
39+
});
40+
});
2341
});
2442
});
2543

src/handleAction.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ function isFunction(val) {
22
return typeof val === 'function';
33
}
44

5-
export default function handleAction(type, reducers) {
6-
return (state, action) => {
5+
export default function handleAction(type, reducers, defaultState) {
6+
return (state = defaultState, action) => {
77
// If action type does not match, return previous state
88
if (action.type !== type) return state;
99

0 commit comments

Comments
 (0)