Lets say whe have this state
state = {
name: 'Daniel',
fruits: {
one: 'Banana',
two: 'Apple'
}
}
I could update this with
this.setState({
fruits: {
...this.state.fruits,
one: 'Pineapple'
}
})
or i could do
this.setState(prevState => ({
fruits: {
...prevState.fruits,
one: 'Pineapple'
}
}))
I understand that if you have a counter that should be incremented, it would be a good idea to make sure you're incrementing from the previous state. But if we are changing a name and don't care about the previous name. Is there any benefit of using prevState in that case?
Do you only need to use prevState if you're actually calculating something based on the previous state? In this case, we are spreading the fruits object. Could this object been changed in another call simultaniously and the version without prevState overriding it? (Do i always have to use prevState when handling objects in state?)