Redux in react

reducer in redux performs a minus one operation

export default (state = initialState, action) => {
  const {total} = state
  switch(action.type){
    case ActionTypes.ADD:
      // 
      return {...state, total: total + 1}
    case ActionTypes.SUBTRACT:
      // 
      return {...state, total: total - 1}
    default:
      return state
  }
}

what is the difference between return {.state, total: total-1} and return state returned by default? what is the role of
.state

Mar.31,2021

because state cannot be modified directly, .state is a copy. {.state, total: total-1} this allows you to modify only the total field in state without affecting other fields in state.

Note state.total = state.total-1; this direct assignment is not allowed by redux, so use the extension operator .

Menu