Sometimes, we want to delete an item using Redux and JavaScript.
In this article, we’ll look at how to delete an item using Redux and JavaScript.
How to delete an item using Redux and JavaScript?
To delete an item using Redux and JavaScript, we return a new array without the removed item.
For instance, we write
function cartReducer(state = initialState, action) {
  const { id } = action.payload;
  switch (action.type) {
    //...
    case actionTypes.DELETE_CART:
      return {
        ...state,
        carts: state.carts.filter((item) => item.id !== payload),
      };
  }
}
to add a switch statement into the cartReducer function.
In it, we check if the DELETE_CART action is invoked.
If it is, then we return an object with the new state that has the removed item gone.
We set carts to a new array that has the item that does have id equal to payload.
Conclusion
To delete an item using Redux and JavaScript, we return a new array without the removed item.
