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', ...
Get React 16 Essentials - Second Edition 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.