“无效的挂钩调用。”当使用react useState Hook时
在研究了许多 Stack Overflow 论坛后,我仍然不知道为什么我的代码不断出现可怕的“Hooks 只能在函数组件的主体内部调用。这可能由于以下原因之一而发生:”错误。
我的代码非常简单且易于理解,我使用一个函数来渲染格式化程序以放入单元格组件中,并且我决定使用 Hooks 来获取是否选中复选框的 React 状态。代码如下:
import React, { useState } from "react"
export function checkbox()
{
const [checked, setCheckbox] = useState(false);
let line_id = this.params.line_id
let checkedValue = ''
let checkedString = ''
if(checked)
{
checkedString = ' checked'
checkedValue = 1
}
return(
<td key={line_id} className='fixedWidth field_checkbox' style={{width: '18px'}}>
<div id={'checkbox'+line_id} className={'text center styled_checkbox'+checked} onClick={() => setCheckbox(prevChecked => !prevChecked)}>
<input type={'checkbox'} name={'id['+line_id+']'} value={lineId} className='styled_checkbox' />
<input type={'hidden'} name={'changed_id['+line_id+']'+line_id} id={'changed_id['+line_id+']'+line_id} />
<input type={'hidden'} name={'checked_id['+line_id+']'+line_id} value={checkedValue} id={'checked_id['+line_id+']'+line_id} />
</div>
</td>
)
}
该函数应该在 html 表的组件中呈现。
After researching many Stack Overflow forums I still don't know why my code keeps getting the dreaded "Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons:" error.
My code is pretty simple and basic to understand, I'm using a function to render formatters to put into a cell component and I decided to use Hooks to get a React state on if a checkbox is checked or not. Here is the code :
import React, { useState } from "react"
export function checkbox()
{
const [checked, setCheckbox] = useState(false);
let line_id = this.params.line_id
let checkedValue = ''
let checkedString = ''
if(checked)
{
checkedString = ' checked'
checkedValue = 1
}
return(
<td key={line_id} className='fixedWidth field_checkbox' style={{width: '18px'}}>
<div id={'checkbox'+line_id} className={'text center styled_checkbox'+checked} onClick={() => setCheckbox(prevChecked => !prevChecked)}>
<input type={'checkbox'} name={'id['+line_id+']'} value={lineId} className='styled_checkbox' />
<input type={'hidden'} name={'changed_id['+line_id+']'+line_id} id={'changed_id['+line_id+']'+line_id} />
<input type={'hidden'} name={'checked_id['+line_id+']'+line_id} value={checkedValue} id={'checked_id['+line_id+']'+line_id} />
</div>
</td>
)
}
This function is supposed to render in a component in a html table.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
发生这种情况是因为您在
div
的 return 语句中使用了checked
值,我放置了
--->
和<---
在错误所在的代码中。It is happening because you are using
checked
value inside the return statement in yourdiv
I've put
--->
and<---
in the code where the mistake is.下面的代码对我来说工作正常
Below code working fine for me
所以我基本上找到了为什么我的 Hook 不起作用。我发现钩子不能在循环内使用,并且我在 .map 函数中启动。对于 React 团队来说,如果能将其放入调试器的错误消息中就好了,但至少我不会再犯同样的错误,并希望论坛帖子能够帮助像我这样刚开始使用 Hooks 的人。
感谢大家的帮助!
So I basically found ou why my Hook wasn't working. I found out hooks can't be used inside loops and I was initiating on in a .map function. Would have been nice for the React team to put it in the error message in the debugger but at least I won't make the same mistake again and hope the forum post will be able to help some other people like me just getting started with Hooks.
Thanks to all of you guys for the help !