Using Redux with React Error in Chapter 20?


#1

First off, LOVE this book, it’s well written and the concepts are really easy to grasp. However I found an issue in Chapter 20 where we build a counter using Redux. On page 261 the switch block in the reducer is written as:

return { count: count + 1 }

But according to the Redux documentation the original state must be immutable. When declaring var count = state.count this is still a pointer to the original state and is changed in the switch block.

Wouldn’t it be better to write it as:
return Object.assign({}, state, {count: count + 1}
This creates a new state object and leaves the original intact so as not to violate the Redux rules of immutability.

Again, great book, super helpful!


#2

That’s a good point, @overtureweb. I’ll mark this as something to fix in a future edition :slight_smile:

Glad you are loving the the book!

Cheers,
Kirupa