Answers for "Update State in react redux reducer"

0

Update State in react redux reducer

case foodConstants.FOOD_SUCCESS_POST: {

  const updatedItems = state.items.map((food1) => {
    if (food1.id === action.id) {
      return { ...action };
    }

    return food1;
  });
 return { ...state, items: updatedItems };
}
Posted by: Guest on June-26-2021
0

Update State in react redux reducer

case foodConstants.FOOD_SUCCESS_POST: {

  let updatedItems = { ...state.items };
  const itemIndex = updatedItems.findIndex((food1) => food1.id === action.id);

  if(itemIndex > -1){
    updatedItems[itemIndex] = {
        ...updatedItems[itemIndex],
        ...action,
    }
  }

 return { ...state, items: updatedItems };
}
Posted by: Guest on June-26-2021

Browse Popular Code Answers by Language