ecommerce_sanity_stripe
ecommerce_sanity_stripe copied to clipboard
Fix for cart reordering while adding quantity.
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)
Very nice approach!