【发布时间】:2021-04-29 15:12:07
【问题描述】:
我很新反应并得到以下错误:
react-dom.development.js:14906 Uncaught 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
我想在提交表单时使用另一个组件。因此,在提交表单时,在文件 A 中,它使用了一个函数,在该函数中,它首先检查某些内容,然后再调用其他组件,例如:
ComponentName()
在组件上我有一些 useState 钩子:
function ComponentName() {
const [state1, setState1] = useState('')
const [state2, setState2] = useState('')
const [state3, setState3] = useState('')
在页面加载的那一刻,我收到了这个错误,有谁知道怎么回事?
【问题讨论】:
-
要显示组件 A 或组件 B(或隐藏/显示组件),请使用条件渲染;你不会自己调用组件函数。
标签: javascript reactjs react-hooks setstate