gpt4 book ai didi

javascript - 当有多个按钮时如何知道点击了哪个按钮(React)

转载 作者:行者123 更新时间:2023-11-28 12:12:53 24 4
gpt4 key购买 nike

我在功能组件中有一个表。表中的每一行的末尾都有一个编辑按钮。当我单击编辑按钮时,它将更改应用于所有编辑按钮。我如何知道单击了哪个按钮并仅对其应用更改? (我有大量行,因此无法为每个按钮创建状态)

Here is a link to my code

最佳答案

您可以做的一种方法是跟踪状态上的行索引。例如,提供 id到按钮,等于行的索引

 <Button id={index} type="button" onClick={e => this.handleEdit(e)}>

在你的 handleEdit 中功能

  handleEdit = e => {
e.preventDefault();
this.setState({
showbutton: true,
showButtonIndex: Number(e.target.id)
});
};

这样您就有了状态中单击的行的索引,并且您可以更改逻辑以显示 <Edit />组件为

{this.state.showbutton && (this.state.showButtonIndex === index) && <Edit />}

我假设您一次只允许编辑一行。

工作代码here

关于javascript - 当有多个按钮时如何知道点击了哪个按钮(React),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56980258/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com