I've been using React for a bit now, and I've always updated an object's state using the spread operator as I remember reading you should directly change current state.
E.g now I write something like this:
const changeState = () => {
setState(prevState => {...prevState, existingKey: 'new value'})
}
But I've just come across a section "State Updates are Merged" in the react docs (https://reactjs.org/docs/state-and-lifecycle.html#state-updates-are-merged). If I'm understanding it correctly it's essentially saying I can just write this instead:
const changeState = () => {
setState({existingKey: 'new value'})
}
and all other keys in the object will remain as is, only the key I have specified will be updated in state. Am I reading this correctly? I've never seen this on stackoverflow, I always see people saying to use spread operators, is there a reason why?