React Hook Form - 带 typescrpit 的动态控制器

问题描述 投票:0回答:1
我希望 fieldName 必须位于控件内部,因此传递表单中不存在的字段名称将不起作用,有人知道如何帮助我吗?

import { Control, Controller } from 'react-hook-form';
interface FieldProps {
  control: Control<any, any>;
  label: string;
  fieldName: string;
}
我试图使用泛型类型,但它对我不起作用

reactjs typescript react-hook-form
1个回答
0
投票
它通常使用泛型类型...RHF 已经提供了它们:

import { Control, Controller, FieldValues, Path } from 'react-hook-form';
interface FieldProps<TFieldValues extends FieldValues = FieldValues> {
  control: Control<TFieldValues>;
  label: string;
  fieldName: Path<TFieldValues>;
}
    
© www.soinside.com 2019 - 2024. All rights reserved.