Learning Redux
上QQ阅读APP看书,第一时间看更新

Separating action types

As we have learned in Chapter 1, Why Redux? reducers use the type property to check which action happened. That works fine, but there is a little problem with the value—it's a string. With string values, it is very easy to make mistakes, such as typos. Furthermore, we will need to know all possible string values (action types), which means that we need to consult the documentation or reducer source files—that's not good.

In Redux projects, we usually store the type property of the action in a constant, as follows:

const CREATE_POST = 'CREATE_POST'

const createPost = { type: CREATE_POST, user: 'dan', text: 'New post' }

console.log(createPost)

The first line defines what is called an action type in the Redux world. We then use this action type to define the createPost action.