I am trying to complete my first React project by myself but I am having some problems. I am mapping a JSON file with each having an input field to add item amount to the cart. The problem is when I change one input value, all the input values change. I want them to change independently. I am trying to use id in this case but I don't know the proper way to put that in the code.
import React, { useState } from "react";
import "./produce.css";
import produce from "../produce.json";
import { MdArrowDropUp, MdArrowDropDown } from "react-icons/md";
const Produce = () => {
const [amount, setAmount] = useState(0);
const handleIncrease = () => {
if (amount < 20) {
setAmount(Number(amount) + 1);
}
};
const handleDecrease = () => {
if (amount > 0) {
setAmount(Number(amount) - 1);
}
};
const handleChange = (e, id) => {
setAmount(e.target.value)
};
return (
<div className="produce">
{produce.map((item) => {
return (
<div className="produce_item" key={item.id}>
<img className="item_img" src={item.img} />
<div className="produce_explain">
<div className="produce_explain1">
<h3>{item.name}</h3>
{item.iconOrganic && <img src={item.iconOrganic} />}
</div>
<div className="produce_explain2">
{item.organic === "organic" && <h5>{item.organic}</h5>}
<div className="produce_weight">
<h6>approx.</h6>
{item.weight}
</div>
</div>
<div className="produce_price">
<h3>{item.price} $</h3>
</div>
</div>
<div className="produce_buy">
<button className="produce_cart">Add to Cart</button>
<div className="produce_number">
<button className="produce_increase" onClick={handleIncrease}>
<MdArrowDropUp />
</button>
<input
type="text"
className="produce_count"
value={amount}
onChange={(e) => handleChange(e, item.id)}
/>
<button className="produce_decrease" onClick={handleDecrease}>
<MdArrowDropDown />
</button>
</div>
</div>
</div>
);
})}
</div>
);
};
export default Produce;
