美文网首页
react-hook-form的使用

react-hook-form的使用

作者: Poppy11 | 来源:发表于2021-07-21 15:28 被阅读0次

推荐使用 "react-hook-form": "^6.3.0",这个版本的form,个人感觉比较好用一点

1、创建表单

 const { handleSubmit, errors, control, setValue } = useForm({
    mode: "all",
    reValidateMode: "onChange",
  });

2、使用

  <form onSubmit={handleSubmit(onSubmit)} className="form">
  <Controller
            key={defaultFormValue?.name}
            name="name"
            defaultValue={defaultFormValue?.name || ""}
            control={control}
            rules={{
              required: true,
            }}
            render={({ value, onChange }) => (
              <input
                onChange={(e) => onChange(e.target.value)}
                onBlur={(e) => {
                  onChange(value);
                }}
                value={value}
                className={errors.name ? "form-input-error" : "form-input"}
                maxLength={5}
                name="name"
                placeholder="请输入姓名"
              />
            )}
          />
</form>

相关文章

  • react-hook-form的使用

    推荐使用 "react-hook-form": "^6.3.0",这个版本的form,个人感觉比较好用一点 ...

  • react-hook-form使用

    官网地址:https://react-hook-form.com/[https://react-hook-form...

  • React-hook-form Demo之外——实际的用法

    control的应用 在react-hook-form的API中,有着control和useControl两个接口...

  • iconfont的使用(下载使用)

    1、下载文件 2、在生命周期中引入项目 beforeCreate () { var domModule = ...

  • Gson的使用--使用注解

    Gson为了简化序列化和反序列化的过程,提供了很多注解,这些注解大致分为三类,我们一一的介绍一下。 自定义字段的名...

  • 记录使用iframe的使用

    默认记录一下----可以说 这是我第一次使用iframe 之前都没有使用过; 使用方式: 自己开发就用了这几个属...

  • with的使用

    下面例子可以具体说明with如何工作: 运行代码,输出如下

  • this的使用

    什么是this? this是一个关键字,这个关键字总是返回一个对象;简单说,就是返回属性或方法“当前”所在的对象。...

  • this的使用

    JS中this调用有几种情况 一:纯粹的函数调用 这是函数的最通常用法,属于全局性调用,因此this就代表全局对象...

  • ==的使用

    积累日常遇到的编码规范,良好的编码习惯,持续更新。。。 日常使用==用于判断的时候,习惯性将比较值写前面,变量写后...

网友评论

      本文标题:react-hook-form的使用

      本文链接:https://www.haomeiwen.com/subject/qxujmltx.html