I want to add the addItem to the cartItems list, but if the id is the same as the item that already in the cartItems, I don't want to add another object, instead it will add the previous amountPrice and orderAmount with the new one.
Is there a way for me to achieve that?
const [cartItems, setCartItems] = useState(cartLocalStorage());
const addToCart = (id, name, image, amountPrice, orderAmount) => {
const addItem = {
id: id,
name: name,
image: image,
amountPrice: amountPrice,
orderAmount: orderAmount,
};
setCartItems([...cartItems, addItem]);
};
};