Can this.setState change multiple properties at once?

Question

In the context of this exercise, can this.setState change multiple properties at once?

Answer

Yes, you can use this.setState to change single, multiple, or even all properties of state at a time. When setting state for multiple properties, only the properties passed in will be updated.

For example, the following shows how the properties key1 and key3 of a state could be updated.

/* Given this state */
this.state = {
  key1: value1,
  key2: value2,
  key3: value3
}

/* We could use code like the following to update specific properties */
this.setState({ key1: newValue1, key3: newValue3 });
10 Likes