React JS-如何在对象内添加对象

and*_*wan 5 state object reactjs

我在构造函数中有以下代码:

constructor() {
    this.state = {
        recipient: {
          lat: -6.173752,
          lon: 106.8925773
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我想将此添加到收件人

var temp = {
  address: 'example street',
  phone: '+623123131321'
}
Run Code Online (Sandbox Code Playgroud)

如何将临时变量添加到收件人

Str*_*ped 7

this.state = {
    recipient: {
        ...temp,
        lat: -6.173752,
        lon: 106.8925773
    }
}
Run Code Online (Sandbox Code Playgroud)

或在构造函数之外,更新状态时:

this.setState(prevState => ({
   recipient: {...prevState.recipient, ...temp}
}))
Run Code Online (Sandbox Code Playgroud)