[英]undefined errors object in react-hook-form
I'm having this issue of我有这个问题
Cannot read property 'title' of undefined.
无法读取未定义的属性“标题”。
I already follow the sample code on the documentation of react-hook-form
but I'm still having this issue.我已经按照
react-hook-form
文档中的示例代码进行了操作,但我仍然遇到这个问题。
const [taskInfo, setTaskInfo] = useState({
title: '',
description: '',
created: currentUser.fullname,
assigned: '',
priority: '',
dateDue: new Date(),
});
const handleChange = (e) => {
const { name, value } = e.target;
setTaskInfo((prev) => ({ ...prev, [name]: value }));
};
const { register, errors, handleSubmit } = useForm();
<TextField
fullWidth
variant='outlined'
label='Title'
value={taskInfo.title}
onChange={handleChange}
onFocus={handleDisbleLabel}
onBlur={handleEnableLabel}
InputLabelProps={{ shrink: false }}
{...register('title', { required: 'This is required' })}
/>
{errors.title && 'Your input is required'}
If you are using react-hook-form
v7, the errors
object is moved to formState
now, change your code to:如果您使用的是
react-hook-form
v7,则errors
object 现在已移至formState
,请将您的代码更改为:
const { register, formState: { errors }, handleSubmit } = useForm();
Instead of代替
const { register, errors } = useForm();
Reference: migrate-v6-to-v7 .参考: migrate-v6-to-v7 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.