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

Decoupling concerns with Flux


First, we'll create a new utility module called WebAPIUtils. Create WebAPIUtils.js in the ~/snapterest/source/utils/ directory:

var SnapkiteStreamClient = require('snapkite-stream-client');
var TweetActionCreators = require('../actions/TweetActionCreators');

function initializeStreamOfTweets() {    SnapkiteStreamClient.initializeStream(TweetActionCreators.receiveTweet);
}

module.exports = {
  initializeStreamOfTweets: initializeStreamOfTweets
};

In this utility module, we first import the SnapkiteStreamClient library and TweetActionCreators. Then, we create the initializeStreamOfTweets() function that initializes a stream of new tweets, just like in the componentDidMount() method of the Stream component, except with one key difference: whenever SnapkiteStreamClient receives a new tweet, it calls the TweetActionCreators.receiveTweet method that passes a new tweet to it as an argument:

SnapkiteStreamClient.initializeStream(TweetActionCreators.receiveTweet);

Remember...