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

Introducing action creators

There is another issue with actions—without documentation, we do not know which properties an action has. To solve this issue, we implement action creators. Action creators are functions that return actions. They can be thought of as an action factory. We tell the function what we want to do, and with which data. The function creates the action, which we can then dispatch to Redux. For example, we could replace our createPost action with a dynamic createPost(user, text) action creator:

function createPost (user, text) {
return {
type: CREATE_POST,
user: user,
text: text
}
}

console.log(createPost('dan', 'New post'))

Now, we can reuse this function to create similar actions. Later, we will learn how to wrap action creators with the Redux dispatch() function. This means that we can simply execute createPost('dan', 'New post') in your UI, and it will create and dispatch a proper CREATE_POST action.