将状态false设置为地图组件中的检查收音机按钮
function EachData({ data, index, open }) {
const [isOpen, setisOpen] = useState(open);
const toggle = (e) => {
setisOpen((prev) => !prev);
};
return (
<tr>
{/*<td></td>'s ... */}
<td>
<div className="functions">
{!isOpen ? (
<>
<label className="far fa-edit" htmlFor={`label-${index + 1}`}>
<input type="radio" name="edit" id={`label-${index + 1}`} onChange={toggle} />
</label>
<label className="far fa-trash"></label>
</>
) : (
<>
<label className="far fa-circle-check"></label>
<label className="far fa-times-circle" htmlFor={`label-${index + 1}`} >
<input type="radio" name="edit" id={`label-${index + 1}`} onChange={toggle} />
</label>
</>
)}
</div>
</td>
</tr>
);
}
export default EachData;
App.js
array.map((data, index)=>{
return(
<EachData data={data} index={index} isOpen={false}/>
)
})
检查无线电按钮时,JSX按预期更改,但是在检查另一个无线电按钮后,前一个人的状态仍然是正确的。如何将这些元素陈述为false?
function EachData({ data, index, open }) {
const [isOpen, setisOpen] = useState(open);
const toggle = (e) => {
setisOpen((prev) => !prev);
};
return (
<tr>
{/*<td></td>'s ... */}
<td>
<div className="functions">
{!isOpen ? (
<>
<label className="far fa-edit" htmlFor={`label-${index + 1}`}>
<input type="radio" name="edit" id={`label-${index + 1}`} onChange={toggle} />
</label>
<label className="far fa-trash"></label>
</>
) : (
<>
<label className="far fa-circle-check"></label>
<label className="far fa-times-circle" htmlFor={`label-${index + 1}`} >
<input type="radio" name="edit" id={`label-${index + 1}`} onChange={toggle} />
</label>
</>
)}
</div>
</td>
</tr>
);
}
export default EachData;
App.js
array.map((data, index)=>{
return(
<EachData data={data} index={index} isOpen={false}/>
)
})
When I check the radio buttons the jsx changes as expected, but after checking another radio button the previous one's state remains true. How do I set those elements state to false ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您应该将
等级
状态存储在array
数据中,而不是everydata
component中You should store
isOpen
state in yourarray
data, not inEachData
component您应该使用
useref
而不是ID,在这里我使用useref
进行一些逻辑,希望这会有所帮助。根据我的理解,当您单击
MAP
功能中的单击“广播”按钮时,这将被激活,当您单击另一个无线电按钮时,上一个单选按钮仍显示为活动,在此代码中,我创建两个USEREF
如您所见,如下代码,所有indexes
都采用了第二个代码,第二个用于删除先前的无线电按钮活动。希望您了解此代码,如果您知道DOM。You should use
useRef
instead of id, here I make some logic withuseRef
I hope this would be helpful.As per my understand, when you click on radio button in
map
function this will be activated and when you click another radio button previous radio button is still showing active, In this code I create twouseRef
as you see the below code, one is taking for allindexes
and second for removing previous radio button active. I hope you understand this code, if you know DOM.您可以用无线电输入的名称设置状态。
参考: http://react.tips/radio-buttons/radio-buttons-in-radio-buttons-in-reactjs/
You can set state with the name of radio input.
Reference: http://react.tips/radio-buttons-in-reactjs/