React应用里Invalid hook call错误消息的处理

发布时间:2025-05-24 16:25:03 作者:益华网络 来源:undefined 浏览量(0) 点赞(0)
摘要:今天我做React开发时,遇到如下的错误消息: > 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 mismatchi

今天我做React开发时,遇到如下的错误消息:

> 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:// fb.me/react-invalid-hoo k-call  for tips about how to debug and fix this problem.

这个错误输出已经提醒了我,出错误的代码在我的应用程序MyApp.jsx里:

原来是我这段const变量初始化代码写到function外部了:

将其移至内部后错误消失:

二维码

扫一扫,关注我们

声明:本文由【益华网络】编辑上传发布,转载此文章须经作者同意,并请附上出处【益华网络】及本页链接。如内容、图片有任何版权问题,请联系我们进行处理。

感兴趣吗?

欢迎联系我们,我们愿意为您解答任何有关网站疑难问题!

您身边的【网站建设专家】

搜索千万次不如咨询1次

主营项目:网站建设,手机网站,响应式网站,SEO优化,小程序开发,公众号系统,软件开发等

立即咨询 15368564009
在线客服
嘿,我来帮您!