redux get payload from dispatch code example
Example 1: redux action
import { createActions, handleActions, combineActions } from 'redux-actions';
const defaultState = { counter: 10 };
const { increment, decrement } = createActions({
INCREMENT: (amount = 1) => ({ amount }),
DECREMENT: (amount = 1) => ({ amount: -amount })
});
const reducer = handleActions({
[combineActions(increment, decrement)]: (state, action) => {
return { ...state, counter: state.counter + action.payload.amount };
}
},
defaultState
);
export default reducer;
Example 2: redux acions
export const incAsyncCreator = createAction("INC");
export const decAsyncCreator = createAction("DEC");
export const incReducer = handleAction(
incAsyncCreator,
(state, action) => ({
...state,
counter: state.counter + 1,
success: action.payload.success
}),
counterState
);
export const decReducer = handleAction(
incAsyncCreator,
(state, action) => ({
...state,
counter: state.counter + 1,
success: action.payload.success
}),
counterState
);
export const { increment, decrement } = createActions({
increment: (payload) => ({ ...payload }),
decrement: (payload) => ({ ...payload })
});
export const counterReducer = handleActions(
{
[increment]: (state, action) => ({
...state,
counter: state.counter + 1,
success: action.payload.success
}),
[decrement]: (state, action) => ({
...state,
counter: state.counter - 1,
success: action.payload.success
})
},
counterState
);