Creating a store
As you learned earlier, stores manage data in your Flux architecture. They provide that data to the React components. We'll create a simple store that manages a new tweet that our application receives from Twitter.
Create a new folder called stores
in our project's ~/snapterest/source/stores
directory. Then, create the TweetStore.js
file in it:
import AppDispatcher from '../dispatcher/AppDispatcher'; import EventEmitter from 'events'; let tweet = null; function setTweet(receivedTweet) { tweet = receivedTweet; } function emitChange() { TweetStore.emit('change'); } const TweetStore = Object.assign({}, EventEmitter.prototype, { addChangeListener(callback) { this.on('change', callback); }, removeChangeListener(callback) { this.removeListener('change', callback); }, getTweet() { return tweet; } }); function handleAction(action) { if (action.type === 'receive_tweet') { ...