Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

To retrieve the value of a checkbox using react-hook-form, you can simply use the "watch" method provided by react-hook-form. The watch method allows you to watch specific fields for changes and retrieve their values.

Assuming that you have a checkbox field with the name "myCheckbox", you can use the following code to retrieve its value:

import { useForm } from "react-hook-form";

function MyComponent() {
  const { register, watch } = useForm();
  const myCheckboxValue = watch("myCheckbox");

  return (
    <form>
      <label>
        <input type="checkbox" name="myCheckbox" ref={register} />
        My Checkbox
      </label>
      <p>{myCheckboxValue ? "Checked" : "Not Checked"}</p>
    </form>
  );
}

In the above code, we're using the "watch" method to retrieve the value of the "myCheckbox" field. The value of myCheckboxValue will be true if the checkbox is checked, and false otherwise.