I have made a shopping cart in react. I have assigned a value to each button. The value shows in the dom so I know it's there.
I created a function to capture the value and then push it into an array. However, when I hit add to cart it say's "nothing added". I've been trying to find a way to listen for the value in the event listener but nothing seems to work.
I have tried putting just e.target.value but the outcome is the same.
Any ideas?
const [item, setItem] = useState([]);
function addCart(e) {
if (e.target.value === "") {
item.push(e.target.value);
console.log(item);
setCart(cart + 1);
} else {
console.log("nothing added");
}
}
<button
value={pro.price}
onClick={addCart}
className="bg-blue-500 text-white font-bold border-white p-2 rounded-md"
>
{"add to cart"}
</button>
setItemthat you're not using.if (e.target.value === "") {- So... only add the value to the array if the value is empty? Ifpro.pricehas any value other than an empty string then this would always invoke theelseblock. Was that just a typo?