Answer the question
In order to leave comments, you need to log in
Why is the checkbox state updated this way?
There is this code:
const rejectedCodes = {unverifiable: {status: false, code: 3}, ...}
const [rejectedStatuses, setRejectedStatuses] = useState(rejectedCodes);
<Checkbox
checked={rejectedStatuses.unverifiable.status}
onChange={(e) => {
setRejectedStatuses({
...rejectedStatuses,
unverifiable: { ...rejectedStatuses.unverifiable, status: e.target.checked }
})
console.log(rejectedStatuses);
}}
>
Unverifiable
</Checkbox>
Answer the question
In order to leave comments, you need to log in
Because rejectedStatuses contains the state at the time of the last render. It cannot be changed by calling setRejectedStatuses, at least because it is a constant. And you check its value immediately after calling the setter function, as if the constant had changed once. It will change the next time the component's function is called, that is, after rendering caused by a state change.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question