Handle an Action in the Store, is the 'Get a Hint' guide correct?

Tell us what’s happening:
Is the guide correct for this issue? I’m reading the “Get a hint” guide page. Here is the link:


It shows the solution as returning defaultState if the action.type is not LOGIN. But the directions say to return the current state. Since defaultState is a const, it’s value will never change. I realize the guide code is simplistic code meant to educate, but returning defaultState seems wrong to me.

I believe the correct sample code should read:

// Same code above, only showing the reducer function.
const reducer = (state = defaultState, action) => {
  // change code below this line
  if (action.type === 'LOGIN') {
    return  {
    login: true
    } 
    } else {
      return state;
  }
  // change code above this line
};
// Same code below.....

Your code so far


const defaultState = {
  login: false
};

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

  // change code above this line
};

const store = Redux.createStore(reducer);

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

If there is agreement that the guide could be improved, I can create a PR.

Thanks,
-Fred

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.13; rv:64.0) Gecko/20100101 Firefox/64.0.

Link to the challenge:

That’s an interesting thing to think through. What is the effect of the const on defaultState when it is assigned to state?

I think the sample code is fine. As long as state is not a const it can freely accept a new reference value. The const only applies to defaultState, and when state is updated by assignment to the defaultState reference value it only sees the new reference value and the sense of constancy from defaultState doesn’t come with it. So that when it comes time to update state again it can take on a the new reference value that is passed to it.

I think this codepen illustrates it:
codepen