React报错之Invalid hook call
正文从这开始~
总览
导致"Invalid hook call. Hooks can only be called inside the body of a function component"错误的有多种原因:
react和react-dom的版本不匹配。- 在一个项目中有多个
react包版本。 - 试图将一个组件作为一个函数来调用,例如,
App()而不是<App />。 - 在类里面使用钩子,或者在不是组件或自定义钩子的函数中使用钩子。
版本匹配
在项目的根目录下打开终端,更新react和react-dom包的版本,确保版本是相匹配的,并且没有使用过时的版本。
# ️ with NPM
npm install react@latest react-dom@latest
# ️ ONLY If you use TypeScript
npm install --save-dev @types/react@latest @types/react-dom@latest
# ----------------------------------------------
# ️ with YARN
yarn add react@latest react-dom@latest
# ️ ONLY If you use TypeScript
yarn add @types/react@latest @types/react-dom@latest --dev
如果错误仍存在,尝试删除node_modules以及package-lock.json(不是package.json)文件,重新运行npm install 并重启你的IDE。
这个错误通常是由于在同一个项目中拥有多个版本的react造成的。
# ️ delete node_modules and package-lock.json
rm -rf node_modules
rm -f package-lock.json
# ️ clean npm cache
npm cache clean --force
npm install
如果错误仍然存在,请确保重启了IDE和开发服务。VSCode经常出现故障,需要重启。
调用组件
这里有另一个示例,用来展示错误是如何发生的。
// App.js
import {useState} from 'react';
// ️ Don't call components as functions ️
App();
export default function App() {
/**
* ️ Warning: Invalid hook call. Hooks can only be
* called inside of the body of a function component.
* This could happen for one of the following reasons:
1. You might have mismatching versions of React and the renderer (such as React DOM)
2. You might be breaking the Rules of Hooks
3. You might have more than one copy of React in the same app
*/
const [count, setCount] = useState(0);
return (
<div>
<h1>Hello world</h1>
</div>
);
}
问题在于,我们将
App组件作为函数进行调用。
你应该只使用具有JSX语法的组件。比如:<App country="Austria" age="30" />,而不是App({country: 'Austria', age: 30})。
确保你没有在一个类组件,或一个既不是组件也不是自定义钩子的函数里面调用钩子。
如果你有一个类,请将其转换为能够使用钩子的函数。
下面是一个例子,说明在一个既不是组件也不是自定义钩子的函数中是如何引起错误的。
// App.js
import {useState, useEffect} from 'react';
// ️ not a component nor custom hook
// so it can't use hooks
function counter() {
const [count, setCount] = useState(0);
useEffect(() => {
console.log('hello world');
}, []);
}
counter函数以小写的c开头,所以它不被React认为是一个组件。因为所有的组件名称必须以大写字母开头。它同样也不是自定义钩子,因为其名称没有以use开头,比如说useCounter。
我们只能在函数组件或自定义钩子里面使用钩子,所以能够使用钩子的一个方法是将counter重命名为useCounter。
import {useState, useEffect} from 'react';
function useCounter() {
const [count, setCount] = useState(0);
useEffect(() => {
console.log('hello world');
}, []);
}
现在React认为useCounter是一个自定义钩子,并允许我们在里面使用钩子。
就像文档中所说的那样:
- 只从React函数组件或自定义钩子中调用Hook
- 只在最顶层使用 Hook
- 不要在循环,条件或嵌套函数中调用 Hook
- 确保总是在你的 React 函数的最顶层以及任何 return 之前使用 Hook
React报错之Invalid hook call的更多相关文章
- Jade报错:Invalid indentation,you can use tabs or spaces but not both问题
现象:通过html生成jade文件之后,更改jade文件时,语句没什么问题的情况下,jade文件编译不通过,报错:Invalid indentation,you can use tabs or spa ...
- react 报错的堆栈处理
react报错 Warning: You cannot PUSH the same path using hash history 在Link上使用replace 原文地址https://reactt ...
- mysql创建表时,设置timestamp DEFAULT NULL报错1067 - Invalid default value for 'updated_at'
问题背景: 线上的linux服务器上的mysql服务器中导出数据库的结构.想要在本地创建一个测试版本 导出后再本地mysql上运行却报错 1067 - Invalid default value ...
- 数据库查询语句报错-ORA-00911: invalid character
数据库查询语句报错-ORA-00911: invalid character 根据自己经验总结下: 1.都是分号惹的祸,有时候sql语句后面有分好导致这种错误 2.还有一种是从别处copy过来的sql ...
- CocoaPods pod install的时候报错:invalid byte sequence in UTF-8 (ArgumentError)解决办法
CocoaPods pod install的时候报错:invalid byte sequence in UTF-8 (ArgumentError)解决办法: 基本可以确定是Podfile中的内容编码有 ...
- ssh调用matplotlib绘图报错RuntimeError: Invalid DISPLAY variable
1.问题:在本地用matplotlib绘图可以,但是在ssh远程绘图的时候会报错 RuntimeError: Invalid DISPLAY variable 2.原因:matplotlib的默认ba ...
- golang解析json报错:invalid character '\x00' after top-level value
golang解析json报错:invalid character '\x00' after top-level value 手动复制字符串:{"files":["c:/t ...
- mybatis报错:Invalid bound statement (not found)
mybatis报错:Invalid bound statement (not found)的原因很多,但是正如报错提示一样,找不到xml中的sql语句,报错的情况分为三种: 第一种:语法错误 Java ...
- git commit -m "XX"报错 pre -commit hook failed (add --no-verify to bypass)问题
在同步本地文件到线上仓库的时候 报错 pre -commit hook failed (add --no-verify to bypass) 当你在终端输入git commit -m "xx ...
随机推荐
- 如何在 Mac 和虚拟机上安装 macOS Big Sur、Monterey 和 Ventura
请访问原文链接:https://sysin.org/blog/how-to-install-macos/,查看最新版.原创作品,转载请保留出处. 作者主页:www.sysin.org 名词解释: 硬件 ...
- 利用Github Action实现Tornadofx/JavaFx打包
原文地址: 利用Github Action实现Tornadofx/JavaFx打包 - Stars-One的杂货小窝 最近开了个新项目,主要是个工具软件,也算个人的自娱自乐吧,也算开源的一部分,想着都 ...
- 2020.10.17【普及组】模拟赛C组 总结
总结 这次比赛 120 分,老师说上 200 是不容易的,但我觉得这不是我真的水平 改题情况 T1 题目大意:有 N 个小朋友,每个小朋友有 \(B_i\) 个朋友,问从中随机选 3 人使得 3 人关 ...
- XDEBUG 选项
到官网 http://www.xdebug.com/download.php 下载 找到对应PHP版本的 Xdebug ,后面带 TS 的为线程安全,本机环境为 win7 64 + php-5.5.1 ...
- JS:eval
定义和用法: eval() 函数计算 JavaScript 字符串,并把它作为脚本代码来执行.eval()函数并不会创建一个新的作用域,并且它的作用域就是它所在的作用域. 如果参数是一个表达式,eva ...
- 从 CPU 讲起,深入理解 Java 内存模型!
Java 内存模型,许多人会错误地理解成 JVM 的内存模型.但实际上,这两者是完全不同的东西.Java 内存模型定义了 Java 语言如何与内存进行交互,具体地说是 Java 语言运行时的变量,如何 ...
- 『忘了再学』Shell流程控制 — 38、while循环和until循环介绍
目录 1.while循环 2.until循环 1.while循环 对while循环来讲,只要条件判断式成立,循环就会一直继续,直到条件判断式不成立,循环才会停止.和for循环的第二种格式for((初始 ...
- ShardingSphere-proxy-5.0.0容量范围分片的实现(五)
一.修改配置文件config-sharding.yaml,并重启服务 # # Licensed to the Apache Software Foundation (ASF) under one or ...
- linux查询文件或者文件夹
查找目录:find /(查找范围) -name '查找关键字' -type d // 查找fastdfs_storage_data文件夹 find / -name fastdfs_storage_da ...
- Linux,Centos系统下配置java Jdk(附下载地址)
一.下载jdk 官网下载地址:https://www.oracle.com/java/technologies/javase/javase-jdk8-downloads.html 需要登录Oracle ...