1import { createActions, handleActions, combineActions } from 'redux-actions';
2
3const defaultState = { counter: 10 };
4
5const { increment, decrement } = createActions({
6 INCREMENT: (amount = 1) => ({ amount }),
7 DECREMENT: (amount = 1) => ({ amount: -amount })
8});
9
10const reducer = handleActions({
11 [combineActions(increment, decrement)]: (state, action) => {
12 return { ...state, counter: state.counter + action.payload.amount };
13 }
14 },
15 defaultState
16);
17
18export default reducer;
1//npm i redux-actions or yarn add redux-actions
2
3// single action creator
4export const incAsyncCreator = createAction("INC");
5export const decAsyncCreator = createAction("DEC");
6
7// single action creator using - single reducer
8export const incReducer = handleAction(
9 incAsyncCreator,
10 (state, action) => ({
11 ...state,
12 counter: state.counter + 1,
13 success: action.payload.success
14 }),
15 counterState
16);
17
18// single action creator using - single reducer
19export const decReducer = handleAction(
20 incAsyncCreator,
21 (state, action) => ({
22 ...state,
23 counter: state.counter + 1,
24 success: action.payload.success
25 }),
26 counterState
27);
28
29
30//multiple action creator
31export const { increment, decrement } = createActions({
32 increment: (payload) => ({ ...payload }),
33 decrement: (payload) => ({ ...payload })
34});
35
36// multiple action creator using - multiple reducer
37export const counterReducer = handleActions(
38 {
39 [increment]: (state, action) => ({
40 ...state,
41 counter: state.counter + 1,
42 success: action.payload.success
43 }),
44 [decrement]: (state, action) => ({
45 ...state,
46 counter: state.counter - 1,
47 success: action.payload.success
48 })
49 },
50 counterState
51);