Creating an action creator
Let's create a new folder called actions
in our project's ~/snapterest/source/actions
directory. Then, we'll create the TweetActionCreators.js
file in it:
import AppDispatcher from '../dispatcher/AppDispatcher'; function receiveTweet(tweet) { const action = { type: 'receive_tweet', tweet }; AppDispatcher.dispatch(action); } export { receiveTweet };
Our action creators will need a dispatcher to dispatch the actions. We will import AppDispatcher
that we created previously:
import AppDispatcher from '../dispatcher/AppDispatcher';
Then, we will create our first action creator receiveTweet()
:
function receiveTweet(tweet) { const action = { type: 'receive_tweet', tweet }; AppDispatcher.dispatch(action); }
The receiveTweet()
function takes the tweet
object as an argument, and creates the action
object with a type
property set to receive_tweet
. It also adds the tweet
object to our action
...