小编Van*_*vig的帖子

基于 React 中的唯一键禁用按钮?

我有为多个项目呈现的多个按钮。所有按钮都有一个我传递给键的唯一 ID,我试图根据唯一 ID 禁用该按钮。禁用布尔值处于状态,当单击按钮时,我希望它禁用那个唯一的按钮。

但是,我的代码禁用了所有呈现的按钮。

我已经使用 map 来访问我所在州的 park items 数组,因此如果我将它们转换为具有该州唯一键的数组,我不确定如何映射按钮。

这是我到目前为止所拥有的:

我的状态:

this.state = {
  parks: [],
  todos: [],
  disabled: false
};
Run Code Online (Sandbox Code Playgroud)

按钮:

<button
 key={item.id} //this id is coming from the mapped array "parks" state
 disabled={this.state.disabled}
 onClick={() =>
    this.setState({
    todos: [...this.state.todos, item.name], //this adds the parks 
                                             //state items to the todos 
                                             //state array
    disabled: true
      })
    }
  >
Run Code Online (Sandbox Code Playgroud)

javascript arrays reactjs react-state

0
推荐指数
1
解决办法
1599
查看次数

标签 统计

arrays ×1

javascript ×1

react-state ×1

reactjs ×1