问题描述: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.的更多相关文章

  1. 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.当然出现这个 ...

  2. React报错之Invalid hook call

    正文从这开始~ 总览 导致"Invalid hook call. Hooks can only be called inside the body of a function compone ...

  3. 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 ...

  4. 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\ ...

  5. JWPlayer Uncaught Error: Invalid SRT file

    错误场景: JWPlayer 播放视频,加入了字幕和缩略图: 字幕为Srt格式: 1 00:00:00,000 --> 00:00:02,000 战略管理过程 2 00:00:03,000 -- ...

  6. gcc编译出现:error: invalid operands to binary & (have ‘char *’ and ‘int *’)

    /************************************************************************* > File Name: ptr_varia ...

  7. python安装locustio报错error: invalid command 'bdist_wheel'的解决方法

    locust--scalable user load testing tool writen in Python(是用python写的.规模化.可扩展的测试性能的工具) 安装locustio需要的环境 ...

  8. mysql5.7下的timestampn Error : Invalid default value for 'timestamp'

    表格创建是爆了个错 Error : Invalid default value for 'timestamp' 参考:http://www.jb51.net/article/71107.htm 这版本 ...

  9. 解决mac升级后,出现的 xcrun: error: invalid active developer path, missing xcrun 错误

    最近升级了mac系统,然后接着写代码就出问题了. 报错信息如下: xcrun: error: invalid active developer path (/Library/Developer/Com ...

  10. 关于在xml文件中的 error: invalid symbol: 'switch' 错误

    在xml布局文件中使用Switch控件时,出现error: invalid symbol: 'switch'报错,代码如下: <Switch android:id="@+id/swit ...

随机推荐

  1. Flex布局专题

    Flex布局专题 参照 https://www.runoob.com/w3cnote/flex-grammar.html 下面是自己看代码的一下 小结,和认识,加笔记,加原文 认识容器 flex布局需 ...

  2. 生成式AI对业务流程有哪些影响?企业如何应用生成式AI?一文看懂

    集成与融合类ChatGPT工具与技术,以生成式AI变革业务流程 ChatGPT背后的生成式AI,聊聊生成式AI如何改变业务流程 ChatGPT月活用户过亿,生成式AI对组织的业务流程有哪些影响? 生成 ...

  3. 使用Hook拦截sendto函数解决虚拟局域网部分游戏联机找不到房间的问题——以文明6为例

    正文 重要提醒(2023-02-13):本文部分内容存在bug,目前正在调试修改,会在一段时间之后更新 重要提醒(2023-02-14):目前已修复主要bug,会在一段时间之后更新,本文计划重写大部分 ...

  4. Symbol详解

    Symbol Symbol是es6引入的一个新的原始数据类型,是一个独一无二的值. 目前为止,js的数据类型有以下几种: 数据类型 说明 undefined undefined null null b ...

  5. LG P5043 树同构

    \(\text{problem}\) 无根树同构的判断 \(\text{Analysis}\) 考虑树哈希,注意使用较正确的哈希方法 无根树同构有个性质 只要判断以这两棵树的重心为根是否同构即可 \( ...

  6. js获取时间戳的方法

    js获取时间戳的方法 转载脚本之家: https://www.jb51.net/article/77066.htm 转载博客园八英里: https://www.cnblogs.com/deepalle ...

  7. Solon2 与 Spring Boot 的区别

    1.与 Springboot 的常用注解比较 Solon 2.2.0 Springboot 2.7.8 说明 @Inject * @Autowired 注入Bean(by type) @Inject( ...

  8. pycharm 关闭符号自动补全

    insert pair bracket 是三种括号的自动补全 insert pair quote 是两种引号的自动补全

  9. appsettings.json用机密替换字符串-利用 VisualStudio 管理用户机密

    以纯文本形式存储密码是不安全的. 例如,存储在中的数据库连接字符串 appsettings.json 可能包含指定用户的 密码: { "ConnectionStrings": { ...

  10. 不可忽略的.gitignore_global

    会不会很奇怪,本地的代码运行没有问题,以为自己提交到远程仓库了 但是别人下载下来却没有,有没有可能是你根本没提交上去,对了,你真的有可能没提交上去 你本地这个文件是灰色的,确实没有提交上去,但是项目中 ...