I'm using React Hook Form. I have a checkbox wrapped in Controller.
- First problem is that onChange event always gives undefined
- Second problem - before the first appeared (unfortunately I don't know after which change) it managed to work but I didn't know how to get current form state - it was updated only after resetting or submitting the form. As you can see I have added a validation rule to the checkbox. I need to enable submit button based on that checkbox so I need to have a current formState immediately.
<Controller
render={({ field: { onChange, value } }) => (
<Checkbox
// checked={value}
onChange={(e) => {
console.log(e.value);
onChange(e.value);
// this print undefined
}}
>
Some text
</Checkbox>)}
control={control} // this changes nothing
defaultValue={false} // I've tried also with default values passed to the FormProvider
name='checkbox'
rules={{
validate: (value: boolean) => value
}} />
e.target.checkedlikeonChange={(e) => onChange(e.target.checked)}instead ofe.value. (*looks like you are using MUI for the Checkbox)