validation - React Form hook - 尽管输入了输入,但仍收到“必填字段”错误消息
问题描述
我刚开始使用 react form hook 构建一个表单。刚从名为 name 的单个输入开始,当我提交表单时,我收到错误消息。
我尝试按照以下说明操作: https ://react-hook-form.com/get-started
//I have tried with this but I get unexpected token error:
<input {...register('name', { required: true })} />
import React from 'react'
import { useForm } from 'react-hook-form'
const NewUserForm = () => {
const { register, handleSubmit, formState: { errors } } = useForm();
const onSubmit = data => console.log(data)
return (
<form onSubmit={handleSubmit(onSubmit)}>
<input type="text" placeholder="Name" name="name" ref={register('name', {required: true})} />
{errors.name && <span> This field is required</span>}
<input type="text" placeholder="Email" name="email" ref={register('email', {required: true})} />
{errors.email && <span> This field is required</span>}
<input type="submit" />
</form>
)
}
export default NewUserForm
解决方案
推荐阅读
- postgresql - Spring Boot JPA @UpdateTimestamp 不适用于 Postgresql
- javascript - React.createElement 预期字符串问题
- firebase - 如何使用 FireBase 身份验证进行 SSO
- ruby-on-rails - 在 rails 中使用 selected_tag 中的选定项目 id
- assembly - 8086 中断 21h , 02h 不打印任何内容
- python - Docker 不提供 Django 静态文件
- java - 限制 url 重定向而不登录到 Java JSP 中的网站?
- google-sheets - 计算列中的出现次数
- opengl - GLSL 中的圆圈被绘制在错误的位置
- powershell - SharePoint Online 和 PNP PowerShell - 在所有元数据中将库文档输出到 CSV,包括内容类型