正文从这开始~

总览

为了解决错误"React Hook 'useEffect' is called in function that is neither a React function component nor a custom React Hook function",可以将函数名的第一个字母大写,或者使用use作为函数名的前缀。比如说,useCounter使其成为一个组件或一个自定义钩子。

这里有个示例用来展示错误是如何发生的。

// App.js

import React, {useEffect, useState} from 'react';

// ️ Not a component (lowercase first letter)
// not a custom hook (doesn't start with use)
function counter() {
const [count, setCount] = useState(0); // ️ React Hook "useEffect" is called in function "counter" that
// is neither a React function component nor a custom React Hook function.
// React component names must start with an uppercase letter.
// React Hook names must start with the word "use".
useEffect(() => {
console.log(count);
}, [count]); return (
<div>
<h2>Count: {count}</h2>
<button onClick={() => setCount(count + 1)}>Increment</button>
</div>
);
}

上述代码片段的问题在于,我们在一个函数中使用了useEffect钩子,而这个函数不是一个组件,因为它以小写字母开头,也不是一个自定义钩子,因为它的名字不是以use开头。

声明组件

如果你想声明一个组件,请将你的函数的第一个字母大写。

// App.js

import React, {useEffect, useState} from 'react';

// ️ is now a component (can use hooks)
function Counter() {
const [count, setCount] = useState(0); useEffect(() => {
console.log(count);
}, [count]); return (
<div>
<h2>Count: {count}</h2>
<button onClick={() => setCount(count + 1)}>Increment</button>
</div>
);
} export default function App() {
return (
<div>
<Counter />
</div>
);
}

函数名必须以大写字母开头,因为这有助于React区分诸如pdivspan等内置元素和我们定义的组件。

就像文档中所说的:

  • 只从React函数组件或自定义钩子中调用Hook
  • 只在最顶层使用 Hook
  • 不要在循环,条件或嵌套函数中调用 Hook
  • 确保总是在你的 React 函数的最顶层以及任何 return 之前使用 Hook

声明自定义钩子

如果你想声明一个自定义钩子,自定义钩子的名称必须以use开头,比如说useCounter

import React, {useEffect, useState} from 'react';

// ️ is a custom hook (name starts with use)
function useCounter() {
const [count, setCount] = useState(0); useEffect(() => {
console.log(count);
}, [count]); return [count, setCount];
} export default function App() {
const [count, setCount] = useCounter(); return (
<div>
<h2>Count: {count}</h2>
<button onClick={() => setCount(count + 1)}>Increment</button>
</div>
);
}

自定义钩子的名字必须以use开头,这样React才能识别你的函数是一个自定义钩子。

总结

为了解决"React Hook 'useEffect' is called in function that is neither a React function component nor a custom React Hook function"的错误,确保只从React函数组件或自定义钩子中调用钩子。

最新文章

  1. 快来熟练使用 Mac 编程
  2. 大牛的博客 osharp以及EF的分析
  3. centos 安装 opencv-3.1.0
  4. iOS指南针
  5. [No000009]学习重要还是经营人脉重要?
  6. Effective C++ -----条款05:了解C++默默编写并调用哪些函数
  7. 10个不太为人所知的,但实用的PHP函数
  8. JavaScript 类定义常用方法(转)
  9. 自动生成Makefile时,关于Makefile.am编写
  10. grep sed
  11. cf442B Andrey and Problem
  12. PHP面试题之小杂鱼
  13. UVALive 5099 Nubulsa Expo 全球最小割 非网络流量 n^3
  14. Java事物基础总结
  15. [分享] 自动化测试与持续集成方案-- UI 检查
  16. html5网页录音
  17. zookeeper安装(集群)
  18. 如何修改启动jupyter的文件路径
  19. Android之动态改变控件大小
  20. 创建对象的一种方式&amp;一种继承机制(代码实例)

热门文章

  1. kafka优劣
  2. ssh打通
  3. Vue引入vuetify框架你需要知道的几点
  4. Linux修改默认ssh22端口
  5. sql server2016 数据库日志 清空语句
  6. k8s之有状态服务部署基石(基础知识)
  7. Tapdata肖贝贝:实时数据引擎系列(三) - 流处理引擎对比
  8. java面向对象编程---方法
  9. C++记录一
  10. idea 错误: 找不到或无法加载主类 xx.xxx.Application