Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
React 16 Essentials

You're reading from   React 16 Essentials A fast-paced, hands-on guide to designing and building scalable and maintainable web apps with React 16

Arrow left icon
Product type Paperback
Published in Nov 2017
Publisher
ISBN-13 9781787126046
Length 240 pages
Edition 2nd Edition
Languages
Tools
Arrow right icon
Authors (3):
Arrow left icon
Christopher Pitt Christopher Pitt
Author Profile Icon Christopher Pitt
Christopher Pitt
Artemij Fedosejev Artemij Fedosejev
Author Profile Icon Artemij Fedosejev
Artemij Fedosejev
Adam Boduch Adam Boduch
Author Profile Icon Adam Boduch
Adam Boduch
Arrow right icon
View More author details
Toc

Table of Contents (14) Chapters Close

Preface 1. What's New in React 16 FREE CHAPTER 2. Installing Powerful Tools for Your Project 3. Creating Your First React Element 4. Creating Your First React Component 5. Making Your React Components Reactive 6. Using Your React Components with Another Library 7. Updating Your React Components 8. Building Complex React Components 9. Testing Your React Application with Jest 10. Supercharging Your React Architecture with Flux 11. Preparing Your React Application for Painless Maintenance with Flux 12. Refining Your Flux Apps with Redux Index

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') {
  ...
lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image