问题描述: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. Zookeeper01 简介和单机安装使用

    1 zookeeper几个关键的东西 1.1 数据结构-节点 /a/b/c 节点 包含了目录和文件的特性(名称类似目录,本身又类似文件携带数据) 1.2 节点分类 永久/临时|有序/无须 特点一:节点 ...

  2. 2023 年 dotnet 仓库社区年度调查已经开始

    准备好今年对 dotnet 指手画脚了吗,平常在群里我忍气吞声,今天我必须重拳出击,快来参与吧. 我直接一个参的加 .NET 是一个跨平台的开发框架,支持多种语言和应用场景,如 C#.F#.VB.NE ...

  3. freeRTOS检测栈溢出

    将FreeRTOSConfig.h里面的configCHECK_FOR_STACK_OVERFLOW设置为2. 随便一个文件里,加入 #include "task.h" void ...

  4. CodeArts Snap:辅助你编程的神器

    摘要:通过将自然语言转化为规范可阅读.无开源漏洞的安全编程语言,提升开发者编程效率,助力企业快速响应市场需求. 本文分享自华为云社区<华为云发布智能编程助手 CodeArts Snap!> ...

  5. [POI2011]MET-Meteors 解题报告

    语言系统紊乱了 QAQ 这道题感觉不是很难鸭 qwq. 先只考虑一个国家,怎么做?很显然,就直接二分一下就行了.判定答案可以维护一个差分数组,然后最后对它做一个前缀和,再求一下这个国家的流行数量就好了 ...

  6. Centos7-RAID冗余磁盘阵列

    转载csdn: Centos7-RAID冗余磁盘阵列_黏住你不让你的博客-CSDN博客

  7. 2023.2.26【模板】扩展Lucas定理

    2023.2.26[模板]扩展Lucas定理 题目概述 求\(\binom {n}{m} mod\) \(p\) 的值,不保证\(p\)为质数 算法流程 (扩展和普通算法毫无关系) 由于\(p\)不是 ...

  8. .net 移动mas短信接口开发

    接口文档下载 1.移动后台接入用户新增(选择HTTP协议) 2.后台代码 /// <summary>    /// 用户名    /// </summary>    priva ...

  9. 为什么不建议使用 @Autowired 注解进行注入

    在Spring中,Bean的注入一般有三种方式:属性注入.set方法注入.构造器注入. 1.Autowired注入的原理 @Autowired属于属性注入,默认按照类型装配,默认情况下要求依赖的对象必 ...

  10. select加下拉箭头

    <div class="flex"> <label>城市</label> <div class="dealerbo"& ...