Redux - Tuong-Nguyen/JavaScript-Structure GitHub Wiki

Concepts

3 principles:

  • ONE immutable Store (unidirectional)
  • Actions trigger the changes
  • Reducers update state

Actions

  • Actions are plan JavaScript objects
  • Must have a type property that indicates the type of action being performed.
rateCourse(rating) {
  return { type: RATE_COURSE, rating: rating };
}
  • rateCourse(rating) {...}: Action creator
  • { type: ..., ...}: Action

Store

  • 1 Store
  • Single responsibility: store data

Create Store

let store = createStore(reducer);

Store API

  • store.dispatch(action): Dispatches an action. This is the only way to trigger a state change.
  • store.subscribe(listener)
  • store.getState(): Returns the current state tree of your application. It is equal to the last value returned by the store's reducer.
  • replaceReducer(nextReducer)

Reducers

Redux Reducers

Redux Flow

Redux flow

State must be Immutable

  • Clarity
  • Performance
  • Amazing debugging
    • Time-travel debugging
    • Undo/Redo
    • Turn off individual actions
    • Play interactions back

Use Immutable.js (https://facebook.github.io/immutable-js/) for ensuring Immutable