I am not sure weather the title of the question is right or not. I run into some problem and i have no idea how to solve them.
In my app there is cart page there is list of items. And each item has a field quantity.which can be updated. My problem Here is when i initiate i state quantity and make two function out of it which increase and decrease the quantity.but the problem is when i click on addQuantity it updates all the products quantity at the same time. I have really no idea to solve this i have been trying to figure out from last couple of days . These are the two items.
These are my codes
class CartContainer extends React.Component { state = { quantity: 1, };
componentDidMount() { this.props.getItemsAddedToCart(); }
render() {
const addItem = () => {
this.setState({
quantity: this.state.quantity + 1,
});
};
const removeItem = () => {
this.setState({
quantity: this.state.quantity - 1,
});
};
Cart.js
return cartData.map((item, index) => { return (
<Grid item xs={4} container justify="space-around">
<CartItemDetails
quantity={quantity}
addItem={addItem}
removeItem={removeItem}
addQty={addQuantity}
subQty={subtractQuantity}
cart={item}
/>
</Grid>
</Grid>
</Grid>
);
CartItemDetail.js
return (
<Grid container className="cart-item-details">
<Grid xs={12} md={4}>
{!this.props.team ? (
<Grid className="counter" xs={12} container>
<Grid container justify="center" alignItems="center" xs={4}>
<button
onClick={this.props.removeItem}
className="decrement"
></button>
</Grid>
<Grid container justify="center" alignItems="center" xs={4}>
<span>{this.props.quantity}</span>
</Grid>
<Grid container justify="center" alignItems="center" xs={4}>
<button
onClick={this.props.addItem}
className="increment"
></button>
</Grid>
</Grid>
) : (
""
)}
Any help would be great I really want to figure this out .
