Handle an Action in the Store 2

Tell us what’s happening:

Did I miss something ? I’m using Chrome on Fedora 28.
Thanks in advance

Your code so far



const defaultState = {
  login: false
};

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

const store = Redux.createStore(reducer);

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


Your browser information:

User Agent is: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/front-end-libraries/redux/handle-an-action-in-the-store/

Uhm, I think it’s a problem of immutability ( not sure if the curriculum went through that yet^^)
Anyway, when you do
currentState = state
you are doing something like
newObj = oldObj
which does not create a new object, the newObj will from now refer to the oldObj instead.
To work around that you can do something like:

 const currentState = Object.assign({}, state);

This way it creates a new object and assign it to the currentState variable ^^

2 Likes

BINGO !

Wasn’t aware of that functioning. I implemented the very workaround you suggested and it works.

Anyway thank you so much !

1 Like