【问题标题】:Will react-hook-form work without the name prop?react-hook-form 会在没有名称道具的情况下工作吗?
【发布时间】:2021-11-09 01:06:33
【问题描述】:

我在 react-hook-form 文档https://react-hook-form.com/api/useform/registerCustom onChange, onBlur看到了这个例子

const firstName = register('firstName', { required: true })
<input 
  onChange={(e) => {
    firstName.onChange(e); // method from hook form register
    handleChange(e); // your method
  }}
  onBlur={firstName.onBlur}
  ref={firstName.ref} 
/>

这里name="firstName"里面没有&lt;input&gt;

那么,如果不提及名称道具,这会起作用吗?

【问题讨论】:

    标签: reactjs react-hook-form


    【解决方案1】:

    name 是必需的,否则 RHF 怎么知道该值属于哪个字段?文档中的代码缺少 name 属性。如果省略,您可以确认代码不起作用:

    <input
      // without the line below you can't see the submitted value
      name={firstName.name}
      onChange={(e) => {
        firstName.onChange(e); // method from hook form register
        handleChange(e); // your method
      }}
      onBlur={firstName.onBlur}
      ref={firstName.ref}
    />
    

    【讨论】:

      【解决方案2】:

      我也遇到了 RHF 在没有 name 属性的情况下无法工作的问题,但是,在当前版本(7.13.0)的文档中,代码示例已经没有 name道具。更新版本为我解决了这个问题。验证延迟也开始起作用(useForm 挂钩中的 delayError 选项)

      【讨论】:

        猜你喜欢
        • 2020-10-20
        • 2011-10-22
        • 2019-03-27
        • 2021-01-05
        • 2017-07-23
        • 1970-01-01
        • 2022-08-21
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多