DEV Community

Discussion on: Redux Basics Explained From A Beginner's Perspective

shmdhussain12 profile image
Mohamed Hussain

I am beginner in Redux, trying to learn, recently I tried to read about redux, then after reading only part of any article about redux , just stopped as there is too much info, Good here is I am able to read the complete article of you, such a clear and simple explanation of redux terms and basic concept.

One doubt, In reducer actions, why we need to spread the state along with the names[] array, (I am thinking we need only the updated state i.e names only )

return {
names: [...names, action.payload]

dylanmesty profile image
Dylan Mestyanek Author

I'm glad you enjoyed the read! Thank you!

So the reason we have to return ...state, as well, is because what you're returning is taking place of your current state. In this case, names is the only thing in state, so it doesn't really matter.

However, say we had another array with ages, or an object in state, in addition to the names array, we'd have to be sure to copy everything from State with ...state, and then change what we need to change.

If there was another array called ages, and the names array, and we only returned names, then ages would be overridden and be gone forever. If that makes sense. :)