Redux: Use const for Action Types

Redux: Use const for Action Types
0.0 0

#1

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


#2

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^^


#3

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


#4

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