update state of 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: update state in react
You have to use setState() for updating state in React
{
hasBeenClicked: false,
currentTheme: 'blue',
}
setState({
hasBeenClicked: true
});
Example 4: reactjs update state example
{
hasBeenClicked: false,
currentTheme: 'blue',
}