What's the best alternative to update nested React state property with setState()?

后端 未结 4 1321
甜味超标
甜味超标 2021-02-08 08:45

I\'ve been thinking about what would be the best way among these options to update a nested property using React setState() method. I\'m also opened to more efficient methods co

4条回答
  •  梦毁少年i
    2021-02-08 09:37

    Use the spread operator

      let {foo} = this.state;
    
      foo = {
        ...foo,
        bar: baz
      }
    
      this.setState({
        foo
      })
    

提交回复
热议问题