Open ashharr opened 1 year ago
Create a copy of the cartItems: const newCartItems = [...cartItems]; // for mutating the array in place
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!
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.