Blog>
Snippets

Action Types as Strings Enforcement

Demonstrate defining action types as constants and using them in action creators to maintain consistency and predictability.
// Action Types
const ADD_TODO = 'ADD_TODO';
const REMOVE_TODO = 'REMOVE_TODO';

// Action Creators
function addTodoActionCreator(todo) {
  return {
    type: ADD_TODO,
    payload: todo
  };
}

function removeTodoActionCreator(todoId) {
  return {
    type: REMOVE_TODO,
    payload: todoId
  };
}
Defines ADD_TODO and REMOVE_TODO as string constants representing the action types and provides two action creators using these constants.