0

i am creating a shopping Cart passing an object on Click, if cart is Empty add the object,

if existing object increment the Quantity

Data:

{ id:4, productName:'Grapes', price:69, quantity:1}

Code

const [cart, setCart] = useRecoilState(cartState)

const addToCart = (object) => {
  if (cart.length == 0) {
    setCart([...cart, object])
  } else {
    cart.filter(items => {
      if (items.id == object.id) {
        // if id matches increment the quantity by 1 
      } else {
        // add the object to existing cart  
        return [...cart, object]
      }
    })
    setCart()
  }
}
Andy
  • 61,948
  • 13
  • 68
  • 95
ASIF KAIF
  • 317
  • 1
  • 4
  • 17

0 Answers0