Redux: Handle an Action in the Store

Redux: Handle an Action in the Store
0.0 0

#1

Tell us what’s happening:

Getting no feedback :frowning:

Your code so far


const defaultState = {
  login: false
};

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

const store = Redux.createStore(reducer);

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

Also tried:

...
  // change code below this line
  switch (action.type) {
    case "LOGIN":
      return { ...state, login: true }
    default:
      return state
  }
  // change code above this line
...

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_2) AppleWebKit/604.4.7 (KHTML, like Gecko) Version/11.0.2 Safari/604.4.7.

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


#2

You are supposed to use a switch so the first snippet definitely wouldn’t work. As for the second example, can you give Object.assign() a go and see if it works? I think it’s quite possible that the editor is not liking the spread operator.


#3

Ok, it works with:

  switch (action.type) {
    case "LOGIN":
      return Object.assign({}, state, {login: true})
    default:
      return state
  }

Thanks for the help @honmanyau!

I think maybe the assignment could be edited to explicitly ask for a switch statement (I don’t see that in the requirements), especially in this case where a single-case switch statement is more verbose and not really more sensible/intuitive a solution than a ternary or if statement. Thanks again, just trying to add constructive criticism.