在复选框上单击两次以更新状态
我正在映射此数组,
array=[
{'id': 1, 'isActive': true, 'name': apple},
{'id': 2, 'isActive': false, 'name': orange},
{'id': 3, 'isActive': false, 'name': forest}
]
项目的图像之后看起来像这样的输入
<input
id={item.id}
type="checkbox"
onClick={() => handleActive(item)}
checked={item.isActiveMarket}
/>
当我单击复选框以激活或脱离活性时,我想在项目中更改
,但是该数组应保持完整,而不会删除任何元素的 任何元素功能这是我得到的
const handleActive = (item) => {
item.isActive = !item.isActive;
};
,但只有当我双击它时
I am mapping through this array
array=[
{'id': 1, 'isActive': true, 'name': apple},
{'id': 2, 'isActive': false, 'name': orange},
{'id': 3, 'isActive': false, 'name': forest}
]
I have an input that looks like this after the map of array
<input
id={item.id}
type="checkbox"
onClick={() => handleActive(item)}
checked={item.isActiveMarket}
/>
I want to change isActive inside the item when i click on the checkbox to activate or desactivate, but the array should stay full without deleting any element
For the function this is what I got
const handleActive = (item) => {
item.isActive = !item.isActive;
};
it works but only when I double click on it
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
尝试使用Onchange而不是OnClick
Try to use onChange instead of onClick
使用以下代码:
Use the below code:
您需要添加
使用效果
才能更新状态:You need to add in a
useEffect
to update the state:似乎反应具有状态同步不当行为。添加密钥道具并将其设置为检查状态,这样,您将能够触发渲染。
It seems like react has a state synchronisation misbehaviour. Add the key prop and set it to the checked state, this way you will be able to trigger the rendering.