gpt4 book ai didi

reactjs - useForm 在既不是 React 函数组件的函数中被调用...错误

转载 作者:行者123 更新时间:2023-12-05 08:25:59 26 4
gpt4 key购买 nike

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

/src/components/competitions/TheVaultform.js Line 5: React Hook "useForm" is called in function "contactform" which is neither a React function component or a custom React Hook function react-hooks/rules-of-hooks

Search for the keywords to learn more about each error.

我刚刚安装了 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>
);
}

最佳答案

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

export default function ContactForm() { ...

关于reactjs - useForm 在既不是 React 函数组件的函数中被调用...错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59499987/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com