dispatch react redux 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: what is dispatch in redux
function dispatch(action) {
if (typeof action !== 'object' || obj === null) {
throw new Error('actions must be plain object.');
}
if (typeof action.type === 'undefined') {
throw new Error('Actions may not have an undefined "type" property.');
}
reducer(currentState, action);
}