小编Max*_*een的帖子

React-hook-form中handleSubmit参数的类型

我在函数中遇到 TypeScript 错误handleSubmit

  1. 我正在handleSubmit通过以下方式检索功能useForm
const {handleSubmit, control, watch, reset} = useForm()
Run Code Online (Sandbox Code Playgroud)
  1. 我正在定义提交功能:
const submit = (data: { file: File, crop: Area }) => {
    onSubmit(data.file, data.crop)  // onSubmit is passed from parent component
}
Run Code Online (Sandbox Code Playgroud)
  1. 我传入handleSubmit组件的onSubmitpropForm
<Form onSubmit={handleSubmit(submit)}>
    // registering form fields here
</Form>
Run Code Online (Sandbox Code Playgroud)
  1. 出现以下 TypeScript 错误:
 Argument of type '(data: {    file: File;    crop: Area;}) => void' is not assignable to parameter of type 'SubmitHandler<FieldValues>'.
[1]   Types of parameters …
Run Code Online (Sandbox Code Playgroud)

typescript reactjs react-hook-form

6
推荐指数
1
解决办法
8105
查看次数

标签 统计

react-hook-form ×1

reactjs ×1

typescript ×1