I'm currently trying to learn React by multiple recent courses.
To update an state, most courses suggest going this way:
const updatedNinjas = [...this.state.ninjas, newNinja];
this.setState({
ninjas: updatedNinjas
});
However, since setState is "asynchronous", the official react documentation recommends to use the previous state and update based on this one.
this.setState(prevState => ({
ninjas: [...prevState.ninjas, newNinja]
}));
Are both solving the same issue (since we use a new array each time in the first example) or is only the last one foolproof?