im trying to create an array of user premissions, which can be controled by a set of checkboxes.
when i check a box and console.log()
the array it looks just fine with all the premissions i checked, but when i uncheck the box, the console.log()
returns the previus array value and it will be ubdated only on the next click
const [prems, setPrems] = useState([]);
const handleCheckbox = (e) => {
let arr = prems
if (e.target.checked) {
arr.push(e.target.value)
setPrems(arr)
console.log(prems) //shwos the array with all the checked values
} else {
let newArr = arr.filter((item) => {
return item !== e.target.value
})
setPrems(newArr)
console.log(prems) //shows the array still with the value i unchecked
}
};
the event on the checkboxes is onChange and it works the same with onClick