I have this way of adding to the cart the different colors for the same products. What I did to remove the color of the same product that was in the cart was to put an or (| |
) in the filter. I just want to know if it is the right way or will this cause an error in the future?
Codesandbox link: Add to Cart Sampled 2 - CodeSandbox
const handleRemove = (product) => {
console.log(product.color, "remove");
const ProductExist = cartItems.find(
(item) => item.id === product.id && item.color === product.color
);
if (ProductExist.quantity === 1) {
setCartItems(
cartItems.filter(
(item) => item.id !== product.id || item.color !== product.color
)
);
} else {
setCartItems(
cartItems.map((item) =>
item.id === product.id && item.color === product.color
? { ...ProductExist, quantity: ProductExist.quantity - 1 }
: item
)
);
}
};