Why use const keywords as action types in Redux?

Tell us what’s happening:
The write-up didn’t provide much information on why this is done, just that it is done. What are the benefits of writing your action types of values assigned with const? What do you gain by having read-only action types?

Your code so far


// change code below this line

// 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/79.0.3945.79 Safari/537.36.

Challenge: Use const for Action Types

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

What’s gonna happen if you suddenly make a typo somewhere in your code like this:

dispatch('LGOUT');
// HINT: your default case will run and nothing is going cause an error and good luck to you to find why your state isn't changing ;)
1 Like