Search code examples
javascriptreact-nativemultidimensional-arraysetstate

unable to access array


here's my state:

state={show: false,addDisplay:true,key:'',sections: [{title:'primary',
                                                         data:['a','b']
                                                      }]
        }

and here I'm trying to setState:

this.setState({...this.state,sections: [{title:'primary',
                                      data: [...this.state.sections.data,this.state.input]
                                       }]
             })

why is this.state.sections.data undefined?


Solution

  • this.state.sections is an array, try with ...this.state.sections[0].data