Ant Design使用Form.useForm()获取数据域报错

468 阅读1分钟

Ant Design框架下在使用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.

最后反复查看Ant Design文档发现有如下提示:

在这里插入图片描述

我使用的是 class component,因此接下来使用ref获取数据域:

经过测试,成功通过ref获取表单数据,并进行重置等操作。

综上所述,在使用class component情况下,应该使用ref方法替代Form.useForm()。