问题描述: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. ChatGPT留给知乎、小红书的时间不多了

    大家好啊,今天我打算给大家整点好活!挑战一下用ChatGPT打入各平台内部. 知乎挑战! 首先了解一下"知乎体" 知乎体是以专业知识为基础,以清晰的条理对问题进行阐述,并解决该问题 ...

  2. 为什么称不坑盒子是wps和word使用者的救世主呢?

    不坑盒子 很多朋友在工作过程中需要对Word文档进行编辑处理,如果想让Word排版更有效率可以试试小编带来的这款不坑盒子软件,这是一个非常好用的插件工具,专门应用在Word文档中,支持Office 2 ...

  3. python新冠疫情分析-国内疫情数据爬取2

    参考资料:04 正则表达式 和 re模块 - www.pu - 博客园 (cnblogs.com) 大佬太牛了 一边查看资料一边备注代码 终于算是看懂了嘿嘿(赶鸭子上架的爬虫一天~) 已经获取到cvs ...

  4. 上午小博(java小知识)

    使用super来引用父类的成分,使用this来引用当前对象 一个类从另一个类继承,new这个子类的实例的时候,使用super来引用父类对象,this是指当前对象的引用. 当new一个对象出来的时候,这 ...

  5. LeetCode HOT 100:乘积最大子数组(动态规划)

    题目:152. 乘积最大子数组 题目描述: 给你一个整数数组,在该数组的所有子数组中,找到一个子数组中所有元素相乘积最大,返回这个最大的积.子数组就是一个数组中,由一个或几个下标连续的元素,组成的小数 ...

  6. 组织炎症水平高的RA患者接受TNF拮抗剂治疗的效果更好

    组织炎症水平高的RA患者接受TNF拮抗剂治疗的效果更好van der Pouw Kraan TC, et al. Ann Rheum Dis. 2008;67(4):563-6.目的:不同患者对TNF ...

  7. 依那西普治疗多关节型和系统型JRA的长期疗效[EULAR2007_SAT0397]

    依那西普治疗多关节型和系统型JRA的长期疗效 Giannini EH, Ilowite NT. EULAR2007. Abstract No:SAT0397. 标签: EULAR2007,EULAR文 ...

  8. Swiper第一页与最后一页禁止滑动

    resistanceRatio抵抗率.边缘抵抗力的大小比例.值越小抵抗越大越难将slide拖离边缘,0时完全无法拖离. mounted: function() { let _this = this; ...

  9. CF846F - Random Query

    题意:对于一个序列,每次随机选择两个数 \(l,r\),如果 \(l\gt r\) 就交换,求 \(l,r\) 中本质不同的数个数的期望. 我们发现,在所有的 \(n^2\) 个选择方案中,其实就是 ...

  10. Vue3 自定义指令执行了两次的问题

    下面是我注册全局指令的代码,这是我注册的一个通过 hljs 解析 html -> pre code 的指令,数据是异步获取的: app.directive("parse-code&qu ...