I'm using react-hook-form and trying to get selected value from custom react-select component, by following the guide: UseFormMethods.
The problem is that no value is being passed to the form.
Im doing the following:
interface Option {
  label: string;
  value: string;
}
interface Props {
  name: string;
  options: Option[];
  error?: string;
  label?: string;
  placeholder?: string;
  disabled?: boolean;
  clearable?: boolean;
}
const MySelect = React.forwardRef<ReactSelect, Props>(
  (
    { name, error, options, label, placeholder, disabled, clearable, ...rest },
    ref
  ) => (
    <>
      {label && <label htmlFor={name}>{label}</label>}
      <ReactSelect
        id={name}
        name={name}
        ref={ref}
        options={options}
        placeholder={placeholder || ""}
        isDisabled={disabled}
        isClearable={clearable}
        {...rest}
      />
      {error && <span>{error}</span>}
    </>
  )
);
export default MySelect;
Full code in codesandbox. Can anyone help me?
have you considered to use Controller?
https://react-hook-form.com/api#Controller
we actually have a working version on the doc as well:
https://codesandbox.io/s/react-hook-form-v6-controller-qsd8r
<Controller
  as={ReactSelect}
  options={[
    { value: "chocolate", label: "Chocolate" },
    { value: "strawberry", label: "Strawberry" },
    { value: "vanilla", label: "Vanilla" }
  ]}
  name="ReactSelect"
  isClearable
  control={control}
/>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With