redux-actions
Search…
combineActions

Methods

combineActions

1
combineActions(...types);
Copied!
Combine any number of action types or action creators. types is a list of positional arguments which can be action type strings, symbols, or action creators.
1
import { combineActions } from 'redux-actions';
Copied!

combineActions(...types)

This allows you to reduce multiple distinct actions with the same reducer.
1
const { increment, decrement } = createActions({
2
INCREMENT: amount => ({ amount }),
3
DECREMENT: amount => ({ amount: -amount }),
4
})
5
6
const reducer = handleAction(combineActions(increment, decrement), {
7
next: (state, { payload: { amount } }) => ({ ...state, counter: state.counter + amount }),
8
throw: state => ({ ...state, counter: 0 }),
9
}, { counter: 10 })
10
11
expect(reducer(undefined, increment(1)).to.deep.equal({ counter: 11 })
12
expect(reducer(undefined, decrement(1)).to.deep.equal({ counter: 9 })
13
expect(reducer(undefined, increment(new Error)).to.deep.equal({ counter: 0 })
14
expect(reducer(undefined, decrement(new Error)).to.deep.equal({ counter: 0 })
Copied!
Below is how you would use combineActions and handleActions together
EXAMPLE
1
const { increment, decrement } = createActions({
2
INCREMENT: amount => ({ amount }),
3
DECREMENT: amount => ({ amount: -amount })
4
});
5
6
const reducer = handleActions(
7
{
8
[combineActions(increment, decrement)]: (
9
state,
10
{ payload: { amount } }
11
) => {
12
return { ...state, counter: state.counter + amount };
13
}
14
},
15
{ counter: 10 }
16
);
17
18
expect(reducer({ counter: 5 }, increment(5))).to.deep.equal({ counter: 10 });
19
expect(reducer({ counter: 5 }, decrement(5))).to.deep.equal({ counter: 0 });
20
expect(reducer({ counter: 5 }, { type: 'NOT_TYPE', payload: 1000 })).to.equal({
21
counter: 5
22
});
23
expect(reducer(undefined, increment(5))).to.deep.equal({ counter: 15 });
Copied!
Last modified 3yr ago
Copy link