doubleyong
管理员
管理员
  • 最后登录2026-05-25
  • 发帖数1198
  • 最爱沙发
  • 喜欢达人
  • 原创写手
  • 社区居民
  • 忠实会员
阅读:8843回复:0

[前端]【react】Error: Hooks can only be called inside of the body of a function component.

楼主#
更多 发布于:2020-08-05 16:45
问题:使用antd组件时,
代码如下 :
const [form] = Form.useForm();

但报如下错误信息:
×
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-hook-call for tips about how to debug and fix this problem.


原因:
注意 useForm React Hooks 的实现,只能用于函数组件,class 组件请查看下面的例子。
如果是在 class component 下,你也可以通过 ref 获取数据域。 请注意官方示例下面的说明


解决方案:

参考antd官网,参于class component下,调用的示例方法,不要选择支持函数的示例代码

图片:react.jpg

知识需要管理,知识需要分享
游客


返回顶部

公众号

公众号