【问题标题】:unable to access array无法访问数组
【发布时间】:2018-06-05 14:49:06
【问题描述】:

这是我的状态:

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

我在这里尝试设置状态:

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

为什么 this.state.sections.data 未定义?

【问题讨论】:

  • 因为this.state.sections 是一个数组,所以您不会像访问数组一样访问它

标签: javascript react-native multidimensional-array setstate


【解决方案1】:

this.state.sections 是一个数组,试试 ...this.state.sections[0].data

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-10-13
    • 1970-01-01
    • 1970-01-01
    • 2010-10-18
    • 2019-06-13
    • 2021-08-06
    • 2013-12-15
    • 1970-01-01
    相关资源
    最近更新 更多