react报错解决 Rendered more fewer hooks than during the previous render
错误
- 此次渲染比上次渲染多出了hook
错误实例代码
点击是否显示Son组件
时候会报错
//父亲组件
const Father = (props) => {const [fatherState,setFatherState] =useState(false)return(<><span onClick={()=>{setFatherState(!fatherState)}}>是否显示Son组件</span>{fatherState?Son():null} //注意这里的子组件使用方法</>)
}//子组件
const Son=()=>{const [sonState,setSonState] =useState(false)return(<><span onClick={()=>{setSonState(!sonState)}}>点击显示/隐藏内容</span>{sonState?'---son的内容---':null}</>)
}export default Father
错误产生的原因
- 这里看到我调用Son组件是这样当做一个普通函数
Son()
进行调用而不是当做<Son/>
组件进行调用 - 且这个被当成普通函数使用的
Son
子组件内使用了react hook,示例中使用了useState
解决方法
使用标准的<Son/>
即函数组件。
虽然使用Son()
这种方法也是可以渲染,函数组件本来就是个函数,返回的的确是<>…</>也能被识别,但是你这当成普通函数用就不能在里面使用hook。
简单就是把函数组件当成普通函数去使用,渲染的时候react认为就是普通的函数违反了原则
- 只在 React Functions 调用 Hooks
- 不要在常规 JavaScript 函数(普通函数)中调用 Hook
同理另外个错误也是一样的道理
我把FatherState默认值改成true,点击是否显示Son组件
时候不就Son消失了吗,Son的useState就没了。
const [fatherState,setFatherState] =useState(true)
Rendered fewer hooks than expected. This may be caused by an accidental early return statement.
react报错解决 Rendered more fewer hooks than during the previous render相关推荐
- react 中使用import()实现按需加载报错 解决方法 --‘import’ and ‘export’ may only appear at the top level
react 中使用import()实现按需加载报错 解决方法 --'import' and 'export' may only appear at the top level 参考文章: (1)rea ...
- React报错:Too many re-renders
React报错:Too many re-renders. React limits the number of renders to prevent an infinite loop. Too man ...
- react报错:Type ‘{}‘ is not assignable to type ‘ReactNode‘
终端出现 Type '{}' is not assignable to type 'ReactNode'报错解决办法: 通过更新 @types/react package.json中 - " ...
- 连接远程服务器CredSSP加密Oracle修正报错解决办法
连接远程服务器CredSSP加密Oracle修正报错解决办法: 打开注册表,快捷输入 "regedit"(类似找命令提示符 输入 cmd 一样)找文件夹 路径:HKLM(缩写)\S ...
- 从零开始在ubuntu上安装和使用k8s集群及报错解决
文章目录 安装docker 安装kubernetes 配置k8s集群 配置虚拟机网络 配置Master节点的k8s网络 拉取k8s需要的镜像 启动 kubeadm 和 kubelet 配置 node节 ...
- Linux/ubuntu 安装 redis 4.0报错解决:redis-server.service: Can't open PID file /var/run/redis/redis-server.
此文首发于我的个人博客:Linux/ubuntu 安装 redis 4.0报错解决:redis-server.service: Can't open PID file /var/run/redis/r ...
- Python-PyCharm 报错解决:ImportError: cannot import name 'InteractiveConsole' from 'code'
此文首发于我的个人博客:Python-PyCharm 报错解决:ImportError: cannot import name 'InteractiveConsole' from 'code' - z ...
- 报错解决:RequestsDependencyWarning: urllib3 (1.24.3) or chardet (3.0.4) doesn't match a supported versio
此文首发于我的个人博客:报错解决 RequestsDependencyWarning urllib3 (1.24.3) or chardet (3.0.4) doesn't match a suppo ...
- 报错解决:ad NaCl helper startup ack (0 bytes)
此文首发于我的个人博客:报错解决:ad NaCl helper startup ack (0 bytes) - zhang0peter的个人博客 在使用pyppeteer操作Chrome进行爬虫时,大 ...
- anaconda -spyder报错解决-UnicodeDecodeError: 'gbk' codec can't decode byte 0x93 in position 611: illegal
此文首发于我的个人博客:anaconda -spyder报错解决-UnicodeDecodeError 'gbk' codec can't decode byte 0x93 in position 6 ...
最新文章
- 手动初始化设置3PAR存储系统
- python代码案例详解-Python编程:案例详解输出函数print
- 利用Git Bash 远程访问服务器
- 如何嵌入Live Messenger?
- 最佳实践:HTAP数据库TBase助力某省级单位核心系统IT架构升级
- 网红证明题:圆周率其实应该是4!看完真的不明觉厉?
- 爱奇艺推出国际娱乐服务iQIYI App:科技和内容同时出海 携手Astro展开马来西亚地区本土化运营...
- Cookie,Session解决用户的会话跟踪问题
- 【原创】大端和小端字节序的细节
- PCB板制造工艺讲解,动图揭秘PCB板生产流程
- 利用SolidWorks解决方案取得健身器材市场的领先地位!
- 查看dmp文件oracle版本,Oracle的DMP文件修改版本号
- 喜讯!清华大学镜像站恢复anaconda加速
- USB接口、手机接口
- 利用Olami SDK 实现语音控制计算器(iOS)
- Quantopian教程系列四
- 红到发紫的人工智能,2019运势如何?
- esp8266_arduino源码
- 0117 - 深耕 iBooks 标注导入
- 【结构生物学】基于AI辅助的结构蛋白质组学研究细胞内的蛋白质复合体
热门文章
- CentOS7查看和关闭防火墙
- 无码科技发布第一款产品:Readhub
- Python(十)函数
- Python图像处理笔记——形态学处理(skimage.morphology)
- Error Client wants topic A to have B, but our version has C. Dropping connection.
- 随手记 第1篇 | 笔记本win10 Edge 如何滚动截屏
- 聊一聊如何把SSL证书安装到小鸟云服务器上
- android 系统 迷你pc,Remix mini:一款真正运行安卓系统的电脑
- Pandoc 安装与使用
- 如何设置苹果手机铃声