Reducer Updating an Array with Item in Middle

When you need to update an item in the middle of an array, you do this:

  switch(action.type) {
    case 'INCREMENT_LIKES' : 
      console.log('incrementing');
      const i = action.index;
      const length = state.length;
      return [
        ...state.slice(0,1),
        {...state[i], likes: state[i].likes + 1},
        ...state.slice(i + length)
      ]
    default:
      return state;
  }
}

Last updated