Search code examples
react-nativereact-reduxreact-stateimmutability-helperreact-native-state

Updating the array object in React state


I have a state as,

 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",
    }]}

I want to update the value of specific seatNo with its id on an event.


Solution

  • try updating state in custom event like this

    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})
    }