当前位置: 首页 > 知识库问答 >
问题:

useForm在既不是React函数组件也不是React函数组件的函数中调用。。。错误

百里疏珂
2023-03-14

下面的代码出现了最奇怪的错误。

/src/components/competitions/TheVault表单。js第5行:在函数“contactform”中调用React钩子“useForm”,该函数既不是React函数组件,也不是定制的React钩子函数React钩子/钩子规则

搜索关键字以了解有关每个错误的更多信息。

我刚刚安装了react hook表单并运行了演示代码。

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个答案

洪昊然
2023-03-14

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

export default function ContactForm() { ...
 类似资料: