1this.setState(prevState => {
2 let jasper = Object.assign({}, prevState.jasper); // creating copy of state variable jasper
3 jasper.name = 'someothername'; // update the name property, assign a new value
4 return { jasper }; // return new object jasper object
5})
6
7//2ND METHOD
8
9this.setState(prevState => ({
10 jasper: { // object that we want to update
11 ...prevState.jasper, // keep all other key-value pairs
12 name: 'something' // update the value of specific key
13 }
14}))
1this.setState(prevState => ({
2 food: {
3 ...prevState.food, // copy all other key-value pairs of food object
4 pizza: { // specific object of food object
5 ...prevState.food.pizza, // copy all pizza key-value pairs
6 extraCheese: true // update value of specific key
7 }
8 }
9}))