react-switch
react-switch copied to clipboard
Integration with react-hook-form
I want to create a switch, that when checked gives a value and when unchecked gives another new value. And whichever the value. I want to use react-hook-form to register it. Is it possible?
Had this same issue and wanted to share a solution based on this: https://stackoverflow.com/a/75616939
import { useForm, Controller } from "react-hook-form";
const { control } = useForm();
<Controller
control={control}
name="MySwitch"
render={({ field }) => {
return (
<Switch
checked={field.value ?? false}
{...field}
/>
);
})
/>