【问题标题】:Problem with changing Array State using React Hooks使用 React Hooks 更改数组状态的问题
【发布时间】:2021-07-11 09:23:46
【问题描述】:

我是 React 新手,在使用 Hooks 更改 Arrays States 中的状态时遇到问题。 我有一个数组状态,如下所示。 如何使用 ,,setFootballersList" 只更改我状态中的一个单元格,而不必编写所有单元格"?

const [footballersList, setFootballersList] = useState([["1",true], ["2",true], ["3",true]]);

感谢您的回答。

【问题讨论】:

  • 请分享所需的输出。我没有得到你想做的事。你指的是什么细胞
  • 我举个例子。我有一个函数 ,,changeFootbalers(x)" 应该可以工作:因为 changeFootballers(0) 将状态从 [["1",true], ["2",true], ["3",true] 更改为 [ ["1",false], ["2",true], ["3",true]

标签: arrays reactjs react-hooks state


【解决方案1】:

您可以使用数组中单元格的索引。例如将其传递给处理程序。

const handleStateChange = (targetIndex)=>{
setFootballersList(footballersList.map((footballer, index)=>{
   if(index ===TargetIndex){
     //return updatedFootballer
   }
   return footballer;
}))
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-04
    • 2020-07-12
    • 1970-01-01
    • 2021-06-16
    • 1970-01-01
    相关资源
    最近更新 更多