I am trying to add the functionality of increasing items in my ecommerce project but when i increase value of one product it increases of all, how can I approach this differently so that it works the way I want it to.(check the stackblitz link for working example).
import React, { useState } from 'react';
const products = [
{ id: 1, clothe: 'pant' },
{ id: 2, clothe: 'shirt' },
{ id: 3, clothe: 'coat' },
];
const Cart = () => {
const [itemCount, setItemCount] = useState(0);
const increaseItem = () => {
setItemCount(itemCount + 1);
};
const decreaseItem = () => {
setItemCount(itemCount - 1);
};
return (
<div>
{products.map((item) => (
<div>
<div> {item.clothe} </div>
<button onClick={() => increaseItem()}>+</button>
<span>{itemCount}</span>
<button onClick={() => decreaseItem()}>-</button>
</div>
))}
</div>
);
};
export default Cart;