9

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?

Zahir Masoodi
8901 gold badge13 silver badges28 bronze badges
asked Nov 8, 2018 at 8:26

3 Answers 3

18

If your new state is calculated based on any value which is already in state - then you should use the second form of setState where you use a function:

this.setState(prevState => ({
 ninjas: [...prevState.ninjas, newNinja]
}));

or even:

this.setState(({ninjas}) => ({
 ninjas: [...ninjas, newNinja]
}));

It's due to state changes are asynchronous and could be batched due to performance reasons.

If you change the state using some variable which is not based on any value from state - you're free to use a simple version:

this.setState({
 answer: 42
});

Regarding your

since we use a new array each time in the first example

indeed you create a new array each time, but you create it using some set of items which can be not relevant by that time when actual state change will be performed by React under the hood.

answered Nov 8, 2018 at 8:33
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for clearing this out! Don't know why downvote tho
One thing I have an issue with now, using the second method, I'm adding items mutliple times after several submits, because It submits newNinja1 first, then on second submit it adds newNinja1 and newNinja2 on top of the ninja list which already contains newNinja1. How to avoid this duplication when using prevState
@Tanckom use the includes function from JavaScript to check if the previous state contains the item you want to add.
0

Setting states with array helped me in pagination cases works without loosing any data from states

this.setState(prevState => ({
 ninjas: [...prevState.ninjas, ...newNinja]
}));
answered Nov 8, 2018 at 8:49

Comments

-3

As you are beginner so i would like to explain you a bit.

in the code below

...this.state.ninjas

these ... is called spread syntax, soo what following code snipet do is to concate the existing state with the new items; what it results in is to re-render the component with updated state.

const updatedNinjas = [...this.state.ninjas, newNinja];
 this.setState({
 ninjas: updatedNinjas
 });
As for as comparision is concerend they both do the same thing, what react documentation suggests is that first approach may fail but it doesn't say it always fail as i have never seen it failing in my 1.5 years of react experience.

answered Nov 8, 2018 at 8:41

1 Comment

I didn't ask for the explanation of spread operators. I know what they do. I was simply asking how react works if it considers both methods or only last one to consider previous state

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.