问题描述: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;

最新文章

  1. ADMM与one-pass multi-view learning
  2. JVM堆内存设置和测试
  3. DNS弹窗广告遭遇
  4. MurmurHash算法:高运算性能,低碰撞率的hash算法
  5. bzoj4402: Claris的剑
  6. SQL_SERVER_2008升级SQL_SERVER_2008_R2办法 (一、升级;二、重新xie载安装)
  7. JQuery中的AJAX参数详细介绍
  8. careercup-递归和动态规划 9.8
  9. aptana 插件离线下载方式
  10. MSSQL 修改数据库的排序规则
  11. 新建HomeController控制器 继承BaseController
  12. OC—可变数组NSMutableArray
  13. 一些实用的JQuery代码片段收集(筛选,搜索,样式,清除默认值,多选等)
  14. Centos6.5DRBD加载失败,系统更换yum源(国内163)
  15. Docker启动Get Permission Denied
  16. ITEXT5.5.8转html为pdf文档解决linux不显示中文问题
  17. luogu P3250 [HNOI2016]网络
  18. 面试:C++实现访问者模式
  19. 【Spring】20、使用TransactionSynchronizationManager在spring事务提交之后进行一些操作。
  20. Nginx rewrite(重写)

热门文章

  1. windows环境下安装es和kibana
  2. Python 内置界面开发框架 Tkinter入门篇 丁
  3. IDEA新手使用教程【详解】
  4. EPICS Archiver Appliance的定制部署1
  5. ajax的原理是什么?如何实现?
  6. Eureka高可用集群服务端和客户端配置
  7. Vulhub 漏洞学习之:Aria2
  8. 由于pom文件中依赖了redis,服务中没用到,微服务健康监控会报redis的错误
  9. Vulnhub:CK-00靶机
  10. 面了几个说自己精通 Vue 的同学,实在一言难尽……