Creating a store

As we learned earlier, stores manage data in your Flux architecture. They provide that data to the React components. We're going to create a simple store that manages a new tweet that our application receives from Twitter.

Create new folder called stores in our project's ~/snapterest/source/stores directory. Then, create the TweetStore.js file in it:

var AppDispatcher = require('../dispatcher/AppDispatcher'); var EventEmitter = require('events').EventEmitter; var assign = require('object-assign'); var tweet = null; function setTweet(receivedTweet) { tweet = receivedTweet; } function emitChange() { TweetStore.emit('change'); } var TweetStore = assign({}, EventEmitter.prototype, { addChangeListener: function (callback) { this.on('change', ...

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.