Home > Back-end >  My React Shopping Cart code block is not working
My React Shopping Cart code block is not working

Time:09-15

When I click on the add to cart button, the first item is added to the cart, but when I click on the same item again, its quantity should be updated, not the whole item. Does anyone, please help me I am confused know what to do

 const addtoCart = (e,newData) => {
//console.log("hello",newData)
//setAddCart(newData)
//console.log("Test",dataStore)
const dataStore = newData
const Item = addcart.find(a => { return a.API === dataStore})
console.log("A",Item)
console.log("Before",dataStore)
if(Item === undefined){
  dataStore.quantity = 1;
  setAddCart(prev => [...prev,dataStore])
  console.log("AfterinIf",dataStore)
}
else{
  Item.quantity =   1;
  const CartUpdate  = addcart.filter(a => { return a.API !== Item.API});
  CartUpdate.push(Item);  
  setAddCart(CartUpdate);
  console.log("Update",CartUpdate)
}


  
}

CodePudding user response:

try changing

Item.quantity =   1;

to

Item.quantity = Item.quantity   1;
  • Related