Book Image

React.js Essentials

By : Artemij Fedosejev
Book Image

React.js Essentials

By: Artemij Fedosejev

Overview of this book

Table of Contents (18 chapters)
React.js Essentials
Credits
Foreword
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

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);
}

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 object, and now every store...