使用 React Hooks 时出现 UseState 错误 [英] UseState Error when utilizing React Hooks
问题描述
我正在使用钩子,但出现此错误
I am using hooks but I get this error
第 25 行:React Hook "React.useState" 在函数 "contact" 中被调用,该函数既不是 React 函数组件,也不是自定义的 React Hook 函数 react-hooks/rules-of-hooks第 26 行:React Hook "React.useState" 在函数 "contact" 中调用,该函数既不是 React 函数组件,也不是自定义 React Hook 函数 react-hooks/rules-of-hooks第 27 行:React Hook "React.useState" 在函数 "contact" 中被调用,该函数既不是 React 函数组件,也不是自定义的 React Hook 函数 react-hooks/rules-of-hooks
Line 25: React Hook "React.useState" is called in function "contact" which is neither a React function component or a custom React Hook function react-hooks/rules-of-hooks Line 26: React Hook "React.useState" is called in function "contact" which is neither a React function component or a custom React Hook function react-hooks/rules-of-hooks Line 27: React Hook "React.useState" is called in function "contact" which is neither a React function component or a custom React Hook function react-hooks/rules-of-hooks
export default function contact() {
const [messageInput, setMessageInput] = React.useState("");
const [email, setEmail] = React.useState("");
const [name, setName] = React.useState("");
const enables =
messageInput.length > 0 &&
email.length > 0 &&
name.length > 0;
return (
<div className="App">
我之前做过一个类似的项目,我使用我的旧项目作为参考.这在我的旧项目中没有发生.
I have done a similar project before and I am using my old project as a reference. This didn't happened in my old project.
推荐答案
钩子规则 lint 规则使用命名约定来推断函数的用途.以 use
开头的函数被假定为钩子.以大写字母开头的函数被假定为组件.contact
两者都不是.将其更改为 Contact
.
The rules-of-hooks lint rule uses naming conventions to infer what functions are for. Functions starting with use
are assumed to be hooks. Functions starting with a capital letter are assumed to be components. contact
is neither. Change it to Contact
.
这篇关于使用 React Hooks 时出现 UseState 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!