Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 使用带有typescript和material ui的react钩子表单显示错误消息的正确方法_Reactjs_Typescript_Material Ui_React Hook Form - Fatal编程技术网

Reactjs 使用带有typescript和material ui的react钩子表单显示错误消息的正确方法

Reactjs 使用带有typescript和material ui的react钩子表单显示错误消息的正确方法,reactjs,typescript,material-ui,react-hook-form,Reactjs,Typescript,Material Ui,React Hook Form,我正在使用带有typescript和MaterialUI的react钩子表单。我想将错误消息作为helperText传递到TextField。我曾尝试使用 helperText={errors.email?.message} 但是typescript抱怨这个任务 Property 'email' does not exist on type 'NestDataObject<FormData>'.ts(2339) 属性“email”在类型“NestDataObject”上不存在。ts

我正在使用带有typescript和MaterialUI的react钩子表单。我想将错误消息作为
helperText
传递到
TextField
。我曾尝试使用
helperText={errors.email?.message}
但是typescript抱怨这个任务

Property 'email' does not exist on type 'NestDataObject<FormData>'.ts(2339)
属性“email”在类型“NestDataObject”上不存在。ts(2339)
我不想从.eslintrc文件中禁用此规则,因为它可能会忽略应用程序中的其他类似问题,这在某些地方可能是需要的。 将react hook表单的错误消息作为helperText分配给material ui组件的正确方法是什么

代码沙盒链接
需要为表单数据定义数据类型并将其传递给“useForm”

type FormData = {
  email: string;
  password: string;
};

const { control, handleSubmit, errors } = useForm<FormData>();
type FormData={
电子邮件:字符串;
密码:字符串;
};
const{control,handleSubmit,errors}=useForm();

更新的沙盒:

需要为表单数据定义数据类型并将其传递给“useForm”

type FormData = {
  email: string;
  password: string;
};

const { control, handleSubmit, errors } = useForm<FormData>();
type FormData={
电子邮件:字符串;
密码:字符串;
};
const{control,handleSubmit,errors}=useForm();

更新的沙盒:

您的
表单数据类型中没有定义电子邮件。没错,但定义电子邮件的适当方式是什么?表单数据来自
react hook表单
package.Oh。谢谢@HunterMcMillen的提示。它正在工作。发布答案。否您在app.tsx的第5行定义了它:
type FormData={firstName:string;lastName:string;}是。更新了我自己的沙箱,但随后又恢复并分叉另一个沙箱作为答案。谢谢您的帮助@HunterMcMillen。您的
表单数据
类型中没有定义电子邮件。没错,但定义电子邮件的适当方式是什么?表单数据来自
react hook表单
package.Oh。谢谢@HunterMcMillen的提示。它正在工作。发布答案。否您在app.tsx的第5行定义了它:
type FormData={firstName:string;lastName:string;}是。更新了我自己的沙箱,但随后又恢复并分叉另一个沙箱作为答案。谢谢您的帮助@HunterMcMillen。谢谢您,先生,在几个小时没有解决方案后,这一切都很好!谢谢你,先生,在几个小时没有解决方案的情况下,这一切都很完美!