Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript Building Applications with React and Redux Actions, Dispatch, and Reducers. Oh my! Intro to Reducers

Dan Avramescu
Dan Avramescu
11,286 Points

Can we also write the REMOVE_PLAYER case like this?

I tried to write the REMOVE_PLAYER case on my own before the teacher, pausing the video, without using the spread operator.

case PlayerActionTypes.REMOVE_PLAYER:
            let nextState = state.slice();
            nextState.splice(action.index,1);
            return nextState;

Is it the same? I think should work the same regarding immutability. Please share your thoughts.

Also the last block again can it be written like

        case PlayerActionTypes.UPDATE_PLAYER_SCORE:
            let next = state.slice();
            next[action.index].score += action.delta; //here I thought we should use the same args from the Scoreboard component
            return next;

Thank you very much

Julien Collet
Julien Collet
5,968 Points

Hello ! I would also be interested by an official answer to your question.

But here are my thoughts : you could have simply assign 'state' to a new variable without applying the slice method on it :

let nextState = state;
nextState.splice(action.index,1);
return nextState;

I think it would be valid to respect the immutability of the initial state, but it's maybe a matter of best practice to protect memory from unecessary variable assignments, while you could return a new array directly, as the teacher did.

Am I right ?