react-switch icon indicating copy to clipboard operation
react-switch copied to clipboard

Integration with react-hook-form

Open pauxiel opened this issue 3 years ago • 1 comments

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?

pauxiel avatar Jul 28 '22 18:07 pauxiel

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}
      />
    );
  })
/>

vf-telwing avatar May 09 '25 15:05 vf-telwing