How to update nested state properties in React

后端 未结 26 2039
野趣味
野趣味 2020-11-21 06:35

I\'m trying to organize my state by using nested property like this:

this.state = {
   someProperty: {
      flag:true
   }
}

But updating

26条回答
  •  终归单人心
    2020-11-21 07:30

    Create a copy of the state:

    let someProperty = JSON.parse(JSON.stringify(this.state.someProperty))
    

    make changes in this object:

    someProperty.flag = "false"
    

    now update the state

    this.setState({someProperty})
    

提交回复
热议问题