【发布时间】:2016-06-27 18:32:00
【问题描述】:
假设我像这样初始化组件的状态:
getInitialState: function(){
return {name: "Bob Smith",
phone: "555-555-5555",
location: "San Francisco, CA",
best-friend: "Bill Jacobson",
favorite-color: "Orange"}
},
然后,如果我只想修改其中一个状态,我被告知以下是禁止的:
this.state.name = "John Smith";
但我应该使用:
this.setState({name: "John Smith",
phone: this.state.phone,
location: this.state.location,
best-friend: this.state.best-friend,
favorite-color: this.state.favorite-color});
必须有更有效的方法来做到这一点。有没有一种简写的方法可以以一种对反应安全的方式仅更改我的状态对象的一个属性?
【问题讨论】: