Skip to content

Instantly share code, notes, and snippets.

@philmill
Last active February 10, 2016 16:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save philmill/ad1095cac158a5080353 to your computer and use it in GitHub Desktop.
Save philmill/ad1095cac158a5080353 to your computer and use it in GitHub Desktop.

3 Main Principles

Single Immutable State Tree

All application state is contained in a single object tree which cannot be modified directly.

Actions

Actions are used to modify the app state tree. They're objects which describe changes. They only require a type key with other info to describe the change. Any data that will be represented by the app tree will get there via actions regardless of source (UX || Network).

{ type: "ADD_ITEM",
  index: 1,
  text: "hello world"
}

Reducer Functions

To describe state mutations, a single function must be written that takes the previous state of the app, the action being dispatched, and returns the next state of the app. This function has to be pure.

Accepts state and action where unknown actions should return the current state. If the state passed as undefined, the initial state should be returned.

Store

Combines these principles by holding onto the current application state object and allowing actions to be dispatched which will manage changes to the state. Can be initiated with createStore by specifying a reducer function.

Methods

  • getstate() returns the current state
  • dispatch({ type: 'SOME_ACTION' }) specify action objects to modify state
  • subscribe() registers a callback which is called when an action is dispatched
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment