freeCodeCamp Challenge Guide: Handle an Action in the Store

Handle an Action in the Store


Solutions

Solution 1 (Click to Show/Hide)
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"
  };
};
7 Likes

Hello :smile:

Not sure if this is the right place for it, but can anyone explain to me why the following doesn’t work please?

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

The challenge says:

Fill in the body of the reducer function so that if it receives an action of type 'LOGIN' it returns a state object with login set to true .

So should we not be updating the state object’s ‘login’ value to true, rather than returning an anonymous object?

Maybe I’m missing something, but thanks if you can help out?

Thanks,
Z

10 Likes