happypoulp / redux-tutorial

Learn how to use redux step by step
3.76k stars 546 forks source link

05_get-state.js Object Spread #22

Closed timotius02 closed 9 years ago

timotius02 commented 9 years ago

Isn't Object Spread a ES7 proposed feature?

And also wouldn't that only work for shallow combining objects or would that not matter if you only shallow copy a nested state tree in a reducer?

happypoulp commented 9 years ago

Hi,

Isn't Object Spread a ES7 proposed feature?

Yes you're right Object Spread is an ES7 feature. I like it a lot so I use it in the tutorial but you don't have to if you feel like it's too experimental...

And also wouldn't that only work for shallow combining objects or would that not matter if you only shallow copy a nested state tree in a reducer?

And you're also correct that in the code given in 05_get-state:

    switch (action.type) {
        case 'SAY_SOMETHING':
            return {
                ...state,
                message: action.value
            }

we're only shallow copying {message: action.value} into our state. This mean that if there were some complex nested object in our state under a "myprop" property, doing:

    switch (action.type) {
        case 'SAY_SOMETHING':
            return {
                ...state,
                myprop: {
                    // ...
                }
            }

Would completely replace state.myprop by our new object (instead of merging them smartly).

Is it clear for you?

timotius02 commented 9 years ago

It would be helpful if you made it clear that it is Object spread from ES7 not ES6 as you wrote at the bottom of 05_get-state.js. I assumed at first it was array spread because you said it was an ES6 feature.

Other than that I was just curious as to how one would approach having a nested object and if this was the correct way.

happypoulp commented 9 years ago

It would be helpful if you made it clear that it is Object spread from ES7 not ES6 as you wrote at the bottom of 05_get-state.js. I assumed at first it was array spread because you said it was an ES6 feature.

Sure, sorry for the confusion, it's fixed now ;)!

Other than that I was just curious as to how one would approach having a nested object and if this was the correct way.

I believe this truly depends on the structure of your state and how you'd like to update / merge it...There are various possibilities and I don't have enough experience yet to tell you which way would work best in 99% of times (and this is not related to Redux). I added few explanations and possible way to go in 05_get-state.js, here...

Hope it helps!