I have some checkboxes, all the options checked are sent to the server as an Array, the problem is that unchecking isn't working so after you check one even if you uncheck it is sent to the server, and if you check and uncheck multiple times the same value will be sent many times, here is the code.
const [time, setTime] = useState([]);
function handleChangeTime(e) {
if (e.currentTarget.checked) {
setTime([...time, e.target.value]);
} else {
const newArray = setTime.filter((item) => item !== e.target.value);
setTime(newArray);
}
}
console.log(time)
<h4>Select the best time:</h4>
<form>
<label>
<input
type="checkbox"
name="time"
onChange={handleChangeTime}
value="9:00" />
9:00 a.m
</label>
<label>
<input
type="checkbox"
name="time"
onChange={handleChangeTime}
value="9:30" />
9:30 a.m
</label>
<label>
<input
type="checkbox"
name="time"
onChange={handleChangeTime}
value="10:00" />
10:00 a.m
</label>
</form>
Thank you all in advance for the help!