Generic form: Why can't I use default form values of type FieldValues
?
#11470
-
I am trying to write a generic The offending code is: import { FieldValues, useForm } from "react-hook-form";
interface BaseFormProps<TFormValues extends FieldValues> {
defaultValues: TFormValues;
}
function BaseForm<TFormValues extends FieldValues>({
defaultValues,
}: BaseFormProps<TFormValues>) {
const { control, handleSubmit } = useForm<TFormValues>({ defaultValues }); // ERROR
} In the line marked with "ERROR", at
Note that typescript also requires |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
defaultValues are not a simple object on RHF side.
However, if you creating BaseForm to isolate hook usage, i would advise to do smth like this, it will allow to pass any attribute of
|
Beta Was this translation helpful? Give feedback.
defaultValues are not a simple object on RHF side.
if you rewrite you BaseFormProps interface like this, it should work
However, if you creating BaseForm to isolate hook usage, i would advise to do smth like this, it will allow to pass any attribute of
useForm
as a prop to your BaseForm