Home > Software engineering >  What type is register from react-hook-form?
What type is register from react-hook-form?

Time:12-22

When using react-hook-form with Typescript, there is a component that sends some props, register being one of them.

The issue is with its type when declared in an interface:

export interface MyProps {
  title: string;
  ...
  register: (string | undefined) => void;
}

Which is the correct way of declaring register here?

Also tried with:

import { RegisterOptions } from 'react-hook-form';
    export interface MyProps {
      title: string;
      ...
      register: RegisterOptions;
    }

CodePudding user response:

Try this.

import { RegisterOptions, UseFormRegisterReturn } from 'react-hook-form';

export interface MyProps {
  title: string;
  ...
  register: (name: string, options?: RegisterOptions) => UseFormRegisterReturn;
}

CodePudding user response:

You can retrieve register type from UseFormReturn.

import { UseFormReturn } from 'react-hook-form';

export interface MyProps {
  title: string;
  register: UseFormReturn['register'];
}

At react-hook-form, we recommend to forward ref instead of passing register method.

  • Related