update state object react code example
Example 1: Updating an object with setState in React
this.setState(prevState => {
let jasper = Object.assign({}, prevState.jasper);
jasper.name = 'someothername';
return { jasper };
})
this.setState(prevState => ({
jasper: {
...prevState.jasper,
name: 'something'
}
}))
Example 2: setstate find opject in state and update
this.setState(prevState => ({
food: {
...prevState.food,
pizza: {
...prevState.food.pizza,
extraCheese: true
}
}
}))
Example 3: reactjs update state example
{
hasBeenClicked: false,
currentTheme: 'blue',
}
Example 4: reactjs update state example
{
hasBeenClicked: false,
currentTheme: 'bluesss',
}s