Skip to content

Instantly share code, notes, and snippets.

@SanthoshVijayabaskar
Last active October 28, 2015 06:31
Show Gist options
  • Save SanthoshVijayabaskar/8cec73000a43f7ce643a to your computer and use it in GitHub Desktop.
Save SanthoshVijayabaskar/8cec73000a43f7ce643a to your computer and use it in GitHub Desktop.
React Flux Concepts

What is Flux?

  • Flux is a architectural pattern with a uni-directional data flow and centralized dispatcher.
  • It was started by Facaebook
  • Flux deals with actions and data changes

Alternatives to Flux

  • Alt
  • Reflux
  • Flummox
  • Marty
  • Fluxxor
  • Delorean
  • Redux
  • NuclearJS
  • Fluxible

The Problem

![mvc](https://cloud.githubusercontent.com/assets/1716894/10780607/3c849dc8-7d65-11e5-8e16-33d648b682ae.png)

Two-way Binding vs Uni-Directional Flow

![uni-two](https://cloud.githubusercontent.com/assets/1716894/10780604/33dcb6ba-7d65-11e5-86eb-f7d6e54094f8.png)

3 Parts of Flux

3parts

The Big Picture

![big-picture](https://cloud.githubusercontent.com/assets/1716894/10781018/7d0d3b62-7d69-11e5-82a3-821a06c87006.png)

Actions

  • Action encapsulates specific events happening in the application (Eg: Save User, Delete Item)
  • Triggered by User Interaction or Server
  • Passes to dispatcher as helper methods
  • Every action payload has Type and Data

payload

Dispatcher

  • All data flows through Dispatcher as centralized hub - Singleton (we have only one dispatcher)
  • Holds list of callbacks
  • Broadcasts payload to registered callbacks
  • Sends actions to stores

Constants

* Keep things organized
* Provide high livel view of what the app actually does

Stores

  • Store is a centralized place where application state and logic is stored. They don't have direct setter methods, they update via callbacks registered with dispatcher.
  • App data store. They hold Application State, logic and data retrival methods
  • Not a model. It Contains model
  • One or many stores in the app
  • Registers Callbacks with the Dispatchers
  • Uses Node's EventEmitter

Structure of the Store

Every store has these common traits (interfaces):

  1. Extend EventEmitter
  2. addChangeListener and removeChangeListener
  3. emitChange

store-dispatcher

Controller View

  • Controller View is nothing but Top Level Component that helps composing child components
  • They Interacts with Stores, When an Store emits a update, the Controller View should pass the state to children as props
  • Holds data in state
  • Its recommended not to nest Controller View (Must be kept at top level)

Flux Chat

chat_flux

Flux API - 5 Functions

  • register(function callback) - Runs store when actions happen
  • unRegister(string id) - Un register store from the specific action
  • waitFor(array ids) - Update Store Order
  • dispatch(object Payload) - Action notifies dispatcher to update the store
  • isDispatching() - Boolean which is true when dispatcher is busy dispatching something

So Flux is Pub-Sub Model ?

Not Quite

Differs in two ways:

  • Every Payload is dispatched to all registered callbacks
  • Callbacks can wait for other callbacks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment