简体   繁体   English

无法将 react-form-hook 绑定到自定义组件

[英]Unable to bind a react-form-hook to custom component

I'm trying to bind a react-form-hook to a custom control(reactprime), but when I bind the control, it indicastes that:我正在尝试将 react-form-hook 绑定到自定义控件(reactprime),但是当我绑定控件时,它表明:

Type 'Control<ConfigureData, any>' is not assignable to type 'Control<FieldValues, any>'.
  The types of '_options.resolver' are incompatible between these types.
    Type 'Resolver<ConfigureData, any> | undefined' is not assignable to type 'Resolver<FieldValues, any> | undefined'.
      Type 'Resolver<ConfigureData, any>' is not assignable to type 'Resolver<FieldValues, any>'.ts(2322)

ConfigureData is the type of my form. ConfigureData 是我的表单类型。

Here is how I declared it:这是我声明它的方式:

type ConfigureData = {
  title: string;
  description: string;
  startHour: number;
  endHour: number;
  isWorkingMonday: boolean;
  isWorkingTuesday: boolean;
  isWorkingWednesday: boolean;
  isWorkingThursday: boolean;
  isWorkingFriday: boolean;
  isWorkingSaturday: boolean;
  isWorkingSunday: boolean;
};
const schema = yup
  .object({
    title: yup.string().required(),
    description: yup.string().required(),
  })
  .required();

  const {
    control,
    register,
    handleSubmit,
    reset,
    formState: { errors },
  } = useForm<ConfigureData>({ resolver: yupResolver(schema), defaultValues: { isWorkingMonday: true } });

and here is how I'm trying to use it:这就是我尝试使用它的方式:

 <Controller control={control} render={({ field, fieldState }) => <InputSwitch></InputSwitch>} />

But it doesn't let me assign control to the control prop.但它不允许我将控制权分配给 control 道具。 Not sure what I'm missing, I didn't find example of usages of the controller with a typed model?不知道我错过了什么,我没有找到 controller 的用法示例,类型为 model?

If I remove all my generic types, it seems to work, but I would like to continue to use them.如果我删除所有泛型类型,它似乎可以工作,但我想继续使用它们。

You can try你可以试试

import { FieldValues } from 'react-hook-form';从 'react-hook-form' 导入 { FieldValues };

const { control, register, handleSubmit, reset, formState: { errors }, } = useForm({ resolver: yupResolver(schema), defaultValues: { isWorkingMonday: true } }) as FieldValues; const { control, register, handleSubmit, reset, formState: { errors }, } = useForm({ resolver: yupResolver(schema), defaultValues: { isWorkingMonday: true } }) as FieldValues;

as作为

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 react-form-hook 的自定义输入不起作用 - Custom input from react-form-hook not working REACT-FORM-HOOK 类型错误:无法读取未定义的属性“类型” - REACT-FORM-HOOK TypeError: Cannot read property 'type' of undefined 带有 react-form-hook 验证值的 Material UI 自动完成未正确更改 - Material UI autocomplete with react-form-hook validation value is not changing properly 使用 react-hook-form 注册第三方/自定义组件 - Register third party/ custom component with react-hook-form React-hook-form:无法成功实现 MUI Select 组件 - React-hook-form: unable to successfully implement MUI Select component React Hook "React.useState" 在 function "form" 中调用,它既不是 React function 组件也不是自定义 React Hook ZC1C425268E68385D1AB5074C17A94F - React Hook "React.useState" is called in function "form" that is neither a React function component nor a custom React Hook function 绑定 select react-hook-form - Bind select react-hook-form 与 react-hook-form 一起使用时,自定义 React 组件不显示输入长度 - Custom React component doesn't show input length when used with react-hook-form React 使用自定义钩子检测组件外的点击 - React detect click outside a component with custom hook 使用自定义钩子提供的数据测试 React 组件 - Testing React component with data provided by custom hook
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM