Creating an action creator

Let's create a new folder called actions in our project's ~/snapterest/source/actions directory. Then, create the TweetActionCreators.js file in it:

var AppDispatcher = require('../dispatcher/AppDispatcher');

function receiveTweet(tweet) {

  var action = {
    type: 'receive_tweet',
    tweet: tweet
  };

  AppDispatcher.dispatch(action);
}

module.exports = {
  receiveTweet: receiveTweet
};

Our action creators will need a dispatcher to dispatch the actions. We will import AppDispatcher that we created previously:

var AppDispatcher = require('../dispatcher/AppDispatcher');

Then, we create our first action creator receiveTweet():

function receiveTweet(tweet) { var action = { type: 'receive_tweet', tweet: tweet }; AppDispatcher.dispatch(action); ...

Get React.js Essentials now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.