在React状态下更新数组对象

时间:2020-04-01 14:46:57

标签: react-native react-redux react-state immutability-helper react-native-state

我的状态为

 this.state = {
tempData: [{
        "data": [{"id": "a1", "seatNo": 0},{"id": "a2", "seatNo": 0}],
        "rowName": "a",
    },{
        "data": [{"id": "b1", "seatNo": 0},{"id": "b2", "seatNo": 0}],
        "rowName": "b",
    }]}

我想用事件的ID更新特定seatNo的值。

1 个答案:

答案 0 :(得分:0)

尝试在这样的自定义事件中更新状态

onclickevent=(eventid)=>{
   let data=[...this.state.tempData]
   data.map(el=>{
   el.data.map(al=>{
       if(al.id===eventid){
        al.seatNo="update the seat number"
       }
   return null
    })
    return null
    })
   this.setState({tempData: data})
}