I created a card in reactJS for ecommerce app. Add to cart button and increment or decrement button also work here in same card. when I click increment button, they perform work accurately and increment number one by one. When I click decrement button, they could not perform task. I want, when I click decrement button, number decrements and stops on zero no negative value shown me.
const [cart, setCart] = useState({});
const handleDeleteToCart = (item) => {
if (cart.item > 0) {
setCart((pre) => ({ ...pre, [item.id]: (pre[item.id] || 0) - 1 }));
}
};
New contributor
Abdu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.