Reducerfunction reducer(state, action) { if (action.type === "ADD") { return { ...state, count: state.count + action.by } } else if (action.type === "MINUS") { return { ...state, count: state.count - action.by } } return state } let initialState = { count: 0 } let actions = [ { type: 'ADD', by: 2 }, { type: 'MINUS', by: 4 }, { type: 'ADD', by: 10 } ] console.log(actions.reduce(reducer, initialState)) |
|