I have two problems with the checkbox
1- When I click on each checkbox, they are correctly added to plantsFill
, but duplicate items are also saved. I want it to be deleted if the plant_id
is duplicated.
2- When I uncheck it, that item is still stored in my plantsFill
and is not deleted
const allPlants = [
{
"plant_id": 1,
"title": "title 1",
},
{
"plant_id": 2,
"title": "title 2",
},
{
"plant_id": 3,
"title": "title 3",
},
];
const handleCheckClick = (e) => {
setPlantsFill([...plantsFill, {plant_id: e.target.value}]);
};
{allPlants.map((t) => {
return (
<div key={t.plant_id}>
<label htmlFor="">
<input
type="checkbox"
value={t.plant_id}
onChange={handleCheckClick}
/>
{t.title}
</label>
</div>
);
})}
The plantsId.0.plant_id field is required.
| this is my append code :plantsFill.forEach((plantFill) => { formData.append("plantsId[]", plantFill); });
– Comfy