2023-03-01 Error: Invalid hook call.Hooks can only be called inside of the body of a function component.
问题描述:rn项目使用钩子useState,详细报错如下:
Error: 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
See https://reactjs.org/link/invalid-hook-call for tips about how to debug and fix this problem.
ERROR Invariant Violation: Module AppRegistry is not a registered callable module (calling runApplication). A frequent cause of the error is that the application entry file path is incorrect.
This can also happen when the JS bundle is corrupt or there is an early initialization error when loading React Native.
ERROR Invariant Violation: Module AppRegistry is not a registered callable module (calling runApplication). A frequent cause of the error is that the application entry file path is incorrect.
This can also happen when the JS bundle is corrupt or there is an early initialization error when loading React Native.
错误:无效的挂钩调用。钩子只能在函数组件的主体内部调用。这可能是由于以下原因之一:
1.React和渲染器的版本可能不匹配(例如React DOM)
2.你可能违反了勾手规则
3.同一应用程序中可能有多个React副本
看见https://reactjs.org/link/invalid-hook-call获取有关如何调试和修复此问题的提示。
错误不变冲突:模块AppRegistry不是已注册的可调用模块(调用runApplication)。该错误的常见原因是应用程序条目文件路径不正确。
当JS包损坏或加载React Native时出现早期初始化错误时,也可能发生这种情况。
错误不变冲突:模块AppRegistry不是已注册的可调用模块(调用runApplication)。该错误的常见原因是应用程序条目文件路径不正确。
当JS包损坏或加载React Native时出现早期初始化错误时,也可能发生这种情况。
圈重点:无效的挂钩调用。
原因:我的useState放错地方了。我的代码如下:
import React, { useEffect, useState } from "react";
import {
View, Text, SafeAreaView
} from "react-native"; const [navList, setNavList] = useState([]); // 看这里,这行代码不应该放在这里,应该放在Home里面 const Home = () => { useEffect(() => {
setNavList(['yi', 'er', 'san']);
}, []); return (
<SafeAreaView>
<View>
{navList.map((item, index) => {
return (
<View>
<Text key={index}>{item}</Text>
</View>
)
})}
</View>
</SafeAreaView>
)
} export default Home;
正确的写法如下:
import React, { useEffect, useState } from "react";
import {
View, Text, SafeAreaView
} from "react-native"; const Home = () => { useEffect(() => {
setNavList(['yi', 'er', 'san']);
}, []); const [navList, setNavList] = useState([]); return (
<SafeAreaView>
<View>
{navList.map((item, index) => {
return (
<View>
<Text key={index}>{item}</Text>
</View>
)
})}
</View>
</SafeAreaView>
)
} export default Home;
2023-03-01 Error: Invalid hook call.Hooks can only be called inside of the body of a function component.的更多相关文章
- error : Hooks can only be called inside of the body of a function component. 依赖包和主包加载多个react 引发冲突问题
1. 结论: 在依赖包和主包的node-modules中,同时install包含react , react-dom 或者react-redux时,跑测试或者启动的时候,显示如下error.当然出现这个 ...
- React报错之Invalid hook call
正文从这开始~ 总览 导致"Invalid hook call. Hooks can only be called inside the body of a function compone ...
- nginx: [error] invalid PID number "" in "/usr/local/nginx/logs/nginx.pid"
iwangzheng.com tty:[0] jobs:[0] cwd:[/opt/nginx/conf] 12:45 [root@a02.cmsapi]$ /usr/local/nginx/sbin ...
- svn Error: post-commit hook failed (exit code 127) with output
Command: Commit Modified: C:\Users\xsdff\Desktop\project\index.html Sending content: C:\Users\xsdff\ ...
- JWPlayer Uncaught Error: Invalid SRT file
错误场景: JWPlayer 播放视频,加入了字幕和缩略图: 字幕为Srt格式: 1 00:00:00,000 --> 00:00:02,000 战略管理过程 2 00:00:03,000 -- ...
- gcc编译出现:error: invalid operands to binary & (have ‘char *’ and ‘int *’)
/************************************************************************* > File Name: ptr_varia ...
- python安装locustio报错error: invalid command 'bdist_wheel'的解决方法
locust--scalable user load testing tool writen in Python(是用python写的.规模化.可扩展的测试性能的工具) 安装locustio需要的环境 ...
- mysql5.7下的timestampn Error : Invalid default value for 'timestamp'
表格创建是爆了个错 Error : Invalid default value for 'timestamp' 参考:http://www.jb51.net/article/71107.htm 这版本 ...
- 解决mac升级后,出现的 xcrun: error: invalid active developer path, missing xcrun 错误
最近升级了mac系统,然后接着写代码就出问题了. 报错信息如下: xcrun: error: invalid active developer path (/Library/Developer/Com ...
- 关于在xml文件中的 error: invalid symbol: 'switch' 错误
在xml布局文件中使用Switch控件时,出现error: invalid symbol: 'switch'报错,代码如下: <Switch android:id="@+id/swit ...
随机推荐
- STM32F0库函数初始化系列:GPIO配置
1 void GPIO_Configuration(void) 2 { 3 RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE); 4 RCC_AHBP ...
- 继承与多态 动手动脑3方法覆盖(override)”的要点
方法覆盖(override)"的要点 方法覆盖要求子类与父类的方法一模一样,否则就是方法重载(overload)! 在子类中,若要调用父类中被覆盖的方法,可以使用super关键字. 以下子类 ...
- 注释、input()、运算符、组织结构(顺序、选择、循环)
注释 单行注释 # 多行注释 将三对引号之间的为多行注释 ''' ''' 中文编码的声明注释 #coding:gbk input()函数 接受来自用户的输入 返回值是str 值的存储 使用=对输入的值 ...
- SpringBoot 整合Seccurity、权限管理
Spring Boot 整合Spring Seccurity 1.创建maven工程 <?xml version="1.0" encoding="UTF-8&quo ...
- 在线工具帮助医生在社区转诊时甄别SpA患者
在线工具帮助医生在社区转诊时甄别SpA患者 Habibi S, et al. Rheumatology 2016. Present ID: 202. 背景:目前已开发了多种转诊策略以优化脊柱关节炎(S ...
- vue api封装 request.js
import axios from 'axios' import { Message, MessageBox } from 'element-ui' import store from '../sto ...
- B树,B-树,B+树和B*树
B树和B-树(平衡多路查找树)规则:1.排序方法:所有结点关键字按递增次序,并遵循左小右大的原则.2.子结点数:非叶子结点的子结点数>1且<=M且M>=2,空树除外(M阶代表一个树的 ...
- LCD1602液晶屏
单片机系统用到的液晶屏(简称LCD)有很多类型,最常用的有两种,一种是1602型(即每行可显示16个字符,同时能显示2行),另一种是12864型(即每行可显示128个点,每列可显示64个点).一般前一 ...
- Python elasticsearch_dsl 报错及解决方法
Exception:maxClauseCount is set to 1024 原因:报错原因是Search限制一个bool查询中最多只能有1024个值或子查询,当超过1024时,会抛出异常. 解决办 ...
- git手记
参照的是http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000这里的,git教程确实写得不 ...