在函数中调用useForm,该函数都不是React函数组件...错误

时间:2019-12-27 11:16:45

标签: reactjs react-hook-form

以下代码是最奇怪的错误。

  

/src/components/competitions/TheVaultform.js第5行:React Hook   在函数“ contactform”中调用“ useForm”,该函数都不是React   函数组件或自定义的React Hook函数   反应钩/钩规则

     

搜索关键字以详细了解每个错误。

我刚安装了react-hook-form并运行了演示代码。

import React from 'react';
import { useForm } from 'react-hook-form';

export default function contactform() {
  const { register, handleSubmit, errors } = useForm();
  const onSubmit = data => console.log(data);
  console.log(errors);

  return (
    <form onSubmit={handleSubmit(onSubmit)}>
      <input type="text" placeholder="First name" name="First name" ref={register({required: true, maxLength: 80})} />
      <input type="text" placeholder="Last name" name="Last name" ref={register({required: true, maxLength: 100})} />
      <input type="text" placeholder="Email" name="Email" ref={register({required: true, pattern: /^\S+@\S+$/i})} />
      <input type="tel" placeholder="Mobile number" name="Mobile number" ref={register({required: true, minLength: 6, maxLength: 12})} />
      <select name="Title" ref={register({ required: true })}>
        <option value="Mr">Mr</option>
        <option value="Mrs">Mrs</option>
        <option value="Miss">Miss</option>
        <option value="Dr">Dr</option>
      </select>

      <input name="Developer" type="radio" value="Yes" ref={register({ required: true })}/>
      <input name="Developer" type="radio" value="No" ref={register({ required: true })}/>

      <input type="submit" />
    </form>
  );
}

1 个答案:

答案 0 :(得分:2)

它不能识别React组件,因为它应该以大写字母开头,因此请将组件更改为:

- (void)layoutSubviews
{
    [super layoutSubviews];

    for (UIView* subview in self.superview.subviews)
    {
        if (subview != self && subview.layer.cornerRadius > 0)
        {
            subview.layer.cornerRadius = self.cornerRadius; // cornerRadius - your custom property
            subview.clipsToBounds = YES;
        }
    }
}