I am clicking on checkbox. When I am checking the checkbox I am trying to update state but it is not updating immediately. I have created an onchange event named handleAllCheckbox.
Here is my code - const [hello, setHello] = useState(false);
const handleAllCheckbox = (e) => {
setHello(!hello);
if (!hello) {
contactList.map((item) => {
Ids.push(item._id)
});
setTheArray(...theArray, Ids);
}
else {
const newIds = []
setTheArray(...theArray, newIds);
}
}
I want if hello is true then theArray must have Ids & if it is false then theArray should be empty.