adrianhajdin / ecommerce_sanity_stripe

Modern Full Stack ECommerce Application with Stripe
https://jsmastery.pro
2.19k stars 675 forks source link

Fix for cart reordering while adding quantity. #92

Open ashharr opened 1 year ago

ashharr commented 1 year ago

Create a copy of the cartItems: const newCartItems = [...cartItems]; // for mutating the array in place

now we increase quantity, in the foundProduct and simply add it in the new array using index to prevent reordering.

    if(value === 'inc') {
      foundProduct.quantity += 1;
      newCartItems[index] = foundProduct;  // prevents reordering
      setCartItems(newCartItems)
graphiiics commented 1 year ago

Very nice approach!