Please help with this Redux issue

I can’t seem to figure out how to format this switch statement properly. Redux seems a bit abstract to me. Please help! :slight_smile:

const defaultState = {
  authenticated: false
};

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

const store = Redux.createStore(authReducer);

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

const logoutUser = () => {
  return {
    type: 'LOGOUT'
  }
};

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/front-end-libraries/redux/use-a-switch-statement-to-handle-multiple-actions

You’re setting defaultState.authenticated, instead of state.authenticated.
Hope this helps!

Thanks for taking a look! I tried that first, I keep getting an error that authenticated is undefined.

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make easier to read.

See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.

Note: Backticks are not single quotes.

markdown_Forums

Oh my bad! Thank you! Its one of my first posts in this thread, won’t make that mistake again :slight_smile:

Your switch statement itself is fine, the problem is with what you need to return from your switch statement, read the instructions again and see if you can figure it out, and yes redux can be confusing at first.

read the note part especially

Note: At this point, don’t worry about state immutability, since it is small and simple in this example. For each action, you can return a new object — for example, {authenticated: true} . Also, don’t forget to write a default case in your switch statement that returns the current state . This is important because once your app has multiple reducers, they are all run any time an action dispatch is made, even when the action isn’t related to that reducer. In such a case, you want to make sure that you return the current state .

Thank you! Got it! I wasn’t returning the object … completely forgot that!