Redux: Use const for Action Types

Tell us what’s happening:

They give me X and told me: "The action creators and the reducer should reference the LOGIN and LOGOUT constants."

I need your help please.

Your code so far


// change code below this line
const LOGIN = 'LOGIN' 
const LOGOUT = 'LOGOUT'
/* I miss here something and i search on it in internet, but, i didn't found answer yet -- Need your helps please */
// change code above this line

const defaultState = {
  authenticated: false
};

const authReducer = (state = defaultState, action) => {

  switch (action.type) {

    case 'LOGIN':
      return {
        authenticated: true
      }

    case 'LOGOUT':
      return {
        authenticated: false
      }

    default:
      return state;

  }

};

const store = Redux.createStore(authReducer);

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

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

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.87 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/front-end-libraries/redux/use-const-for-action-types

You’re not missiing something up there, the problem is outside the // change code above this line comment ( it’s definitely confusing :/)

Then, edit the authReducer() and the action creators to reference these constants instead of string values.

You’re supposed to change the reference: where the code was using the string literal now you must replace it with the variable name^^

1 Like

Hi @Layer,

Thank you for your explanation, but, i try to fix the error and not working yet.

Here is my new code:

// change code below this line
const LOGIN = 'LOGIN' 
const LOGOUT = 'LOGOUT'
/* I miss here something and i search on it in internet, but, i didn't found answer yet -- Need your helps please */
// change code above this line

const defaultState = {
  authenticated: false
};

const authReducer = (state = defaultState, action) => {

  switch (action.type) {

    case LOGIN:
      return {
        authenticated: true
      }

    case LOGOUT:
      return {
        authenticated: false
      }

    default:
      return state;

  }

};

const store = Redux.createStore(authReducer);

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

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

I don’t know what is the problem, i am pinned here from yesterday, please i need help.

Regards,
Ali Mosaad

Hi all,

I solved the problem and here is the solution:

// change code below this line
const LOGIN = 'LOGIN' 
const LOGOUT = 'LOGOUT'
/* I miss here something and i search on it in internet, but, i didn't found answer yet -- Need your helps please */
// change code above this line

const defaultState = {
  authenticated: false
};

const authReducer = (state = defaultState, action) => {

  switch (action.type) {

    case LOGIN:
      return {
        authenticated: true
      }

    case LOGOUT:
      return {
        authenticated: false
      }

    default:
      return state;

  }

};

const store = Redux.createStore(authReducer);

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

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

Good luck for every one and have a happy coding.

Regards,
Ali Mosaad

2 Likes

you should reference these constants instead of string values. in authReducer() and the action creators

LOGIN -> const instead of ‘LOGIN’ string
LOGOUT->const instead of ‘LOGOUT’ string

The problem is that the editor didn’t mention all the places where
you need to edit the code line, which sort of makes it confusing.
Here you go with corrections:

<redacted>

It is great that you solved the challenge, but instead of posting your full working solution, it is best to stay focused on answering the original poster’s question(s) and help guide them with hints and suggestions to solve their own issues with the challenge.

We are trying to cut back on the number of spoiler solutions found on the forum and instead focus on helping other campers with their questions and definitely not posting full working solutions.

Also, there is no reason to reply to a post that has not had discussions for over a year.

Thank you for understanding.