AsZik
AsZik

Reputation: 641

how to add the quantity to existing product in redux?

I'm writing add to cart function it working perfectly the problem is when I want to add the quantity to existing product based on the id. I'm using redux-sauce to handle the reducer. How to handle the quantity of an existing product.

let exist = state.products.find(v=> v.id === item.id )
export const addToCart = (state, { item }) => {
  return {
    ...state,
    products: [...state.products, { ...item, qty: 1 }],
  };
};

Thanks!!!

Upvotes: 0

Views: 103

Answers (1)

Santiago Betancur
Santiago Betancur

Reputation: 158

I suggest you to create another action, so you can

const addItemsToCart = (state, { items }) => ({
  ...state,
  products: [...state.products, ...items],
});

Otherwise, you'd need to refactor your previous action and the places from where you're calling it

Upvotes: 1

Related Questions