EXCITING! redux reducer question! objects and state and spreading! EXCITING!

case 'favoriteRecipes/removeRecipe':
  return {
    ...state,
    favoriteRecipes: state.favoriteRecipes.filter(element => element.id !== action.payload.id)
  };


case 'favoriteRecipes/removeRecipe':
  return {
    ...state,
    favoriteRecipes: [favoriteRecipes.filter(item => item !== action.payload)]
  }

the second paste is incorrect, the first paste is correct, i vaguely understand this but… barely.

so the way I look at these reducer cases is we spread an existing copy of state, then after that we list a key in the state object that we want to update

but if you see …state, favoriteRecipes

favoriteRecipes knows were still in the state object somehow, whereas its neccesary to include “state.favoriteRecipes” in the filter statement? why is that?

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.