I have a functional component and I'm trying to get the value from a checkbox input, but even if I set a initial state the first value that I get from the console is undefined. It only starts to retrieve the value from the state after I click a second time.
const Preferences = () => {
const [pref, setPref] = useState({
webcam: true
});
const onChange = e => {
setPref({ ...pref, [e.target.name]: e.target.checked });
console.log(webcam); //always undefined on first time
};
...
<input
type="checkbox"
name="webcam"
value={webcam}
onChange={onChange}
defaultChecked={webcam}
/>
pref.webcam?