State Mutation - Redux

Tell us what’s happening:

So I have submitted the below code for the challenge: “Redux: Handle an Action in the Store”. This has been accepted as a valid solution. However, would I be right in thinking that this is bad code, as it mutates the state object directly and hasn’t returned a new copy of the state, or am I wrong?

Just looking for some guidance!

Your code so far


const defaultState = {
  login: false
};

const reducer = (state = defaultState, action) => {
  // change code below this line
  switch(action.type) {
    case 'LOGIN':
      return {login: true}
    default: return state;
  }
  // change code above this line
};

const store = Redux.createStore(reducer);

const loginAction = () => {
  return {
    type: 'LOGIN'
  }
};

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36.

Link to the challenge:
I’M NOT ALLOWED TO USE LINKS, SO I HAVE SPACED THIS OUT A BIT
learn.freecodecamp[DOT]org/ front-end-libraries/ redux/ handle-an-action-in-the-store

You haven’t mutated it. You return a new and different object in the case of ‘LOGIN’ and you return the untouched state object in the default case. It looks fine to me, but I’m still learning this stuff myself so I could be wrong.