Use const for Action Types HELP

Tell us what’s happening:

Your code so far


// change code below this line
const LOGIN = 'LOGIN';
const LOGOUT = 'LOGOUT';
// 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/72.0.3626.121 Safari/537.36.

Link to the challenge:

The problem is that you are explicitly writing out the strings instead of using the constants you created at the top of you file.

For example you have:

    case 'LOGIN':
      return {
        authenticated: true
      }

You should be using the constant there. For example if I had a delete action, I would have:

const DELETE = 'DELETE';

//...

    case DELETE:
      // whatever we need to do to delete

// ...

const DELETE = () => {
  return {
    type: DELETE
  }
};

Notice that I’m using the constant and not the string. You have to change this is four places - twice in your reducer and once for each action creator.

Why do it this way? I thought it was silly at first especially because they basically say the same thing. But there are very good reasons for it, not least of which that they are less error prone and more maintainable.

1 Like

still am not able to pass all the test case , last test case is not accepting

When I changed those four lines to your code, it passed for me. If it still doesn’t for you, can you post the exact code you are currently trying?

// change code below this line
const LOGIN = 'LOGIN';
const LOGOUT = 'LOGOUT';
// 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'
  }
};

Am constantly getting this error

// running tests
The action creators and the reducer should reference the LOGIN and LOGOUT constants.
// tests completed

Good, you fixed the reducers. Now you need to fix the action creators:

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

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

You need to make a similar change here. That’s what the error message is telling you: “The action creators and the reducer should reference the LOGIN and LOGOUT constants.” You are still using strings in your action creators, not the constants.

2 Likes

Thank you so much sir
You are awesome