Quantcast
Channel: Recent Questions - Stack Overflow
Viewing all articles
Browse latest Browse all 12141

How to stop decrement by using function and useState in react JS

$
0
0

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 }));    }  };

Viewing all articles
Browse latest Browse all 12141

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>