Suppose I initialize the state of a component as follows:
getInitialState: function(){
return {name: "Bob Smith",
phone: "555-555-5555",
location: "San Francisco, CA",
best-friend: "Bill Jacobson",
favorite-color: "Orange"}
},
Then, if I just want to change one of these states, I was told that the following: no-no:
this.state.name = "John Smith";
But I have to use:
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});
There must be a more efficient way to do this. Is there a shorthand way to change one property of my state object so that it is safe to react?
source
share