Redux

REDUX

all you really need to write are reducers (to do things) and containers (to get things)!?

before you get to react, know redux

before you get to redux, know flux

this is about redux

redux

problem: flux is almost perfect BUT

problem 1: the code for stores can't be reloaded without wiping out the state

When you reload the store object to see the effect that the new state change logic has, you lose the state that the store is holding on to. Plus, you mess up the event subscriptions that tie the store to the rest of the system.

solution 1: separate store into two functions

problem 2: the state is being rewritten with every action

solution 2: when action comes into store, copy state and make changes to the copy

problem 3: there aren't good places for third-party plugins to jump in

solution 3: make it easy to wrap parts of the system in other objects

solution 3+: use a tree to structure the state change logic

other miscellaneous improvements

solution: redux - flux + hot reloading + time travel debugging + misc

redux as a better flux: unidirectional information flow

  1. action (almost same as flux)
  2. dispatcher (DOES NOT EXIST IN REDUX)
  3. store (also does dispatching)
  4. view (different structure)
  5. view layer binding (new in REDUX)
  6. goes back to 1.
  7. ++: the root component (because it is React/Flux's root component with additional responsibilities)

describing redux as a team of people working together, but a slightly different team compared to flux

1. the ACTION creator

FLUX: takes input, turns it into actions, passes it to dispatcher

REDUX: takes input, turns it into actinos, returns formatted action object

FLUX

There’s a neat side effect to having a part of your system that knows all of the possible actions. A new developer can come on the project, open up the action creator files and see the entire API — all of the possible state changes — that your system provides.

REDUX (changes)

2. the DISPATCHER

takes an action, passes it to all the stores registered to it, handles dependencies (pass to store 1, wait for result, then pass to store 2)

controls when to tell which store to do what

FLUX

REDUX : none

3. the STORE

go through all actions taken from DISPATCHER, make changes to state based on what it cares about, and tell CONTROLLER VIEW that the state has changed

only does stuff that they care about, in their own time

FLUX : many stores

REDUX: one store

Those actions will be then passed to the ALL of the reducers combined into our rootReducer and each one of them will return a new copy of the state modified (or not) according to the requested action.

STORE --passes current state--> ROOT REDUCER --passes to relevant reducers--> REDUCERS copy and make changes to copy --pass changed copy back--> ROOT REDUCER --passes new state back--> STORE that makes it the official new state

REF

comment

4. the CONTROLLER VIEW and the VIEW

controller view is the logic, view is the display endpoint

when the state has changed, take new state, turn state into html and pass it to the views.

FLUX : controller views and regular views

REDUX : smart and dumb components

dumb components == standard React component
smart components / containers == dumb components + connect()

5. view layer binding

introduces 3 concepts

concept 1: the Provider component

concept 2: connect()

example :
ACTION CREATOR here takes new input and returns a formatted ACTION
then the VIEW LAYER BINDING takes the formatted ACTION and the
SMART COMPONENT (also CONTAINER) and plugs them together, so now the
SMART COMPONENT HAS THE ACTION.

the VIEW LAYER BINDING does this (with its connect() function):
connect(mapStateProps, mapDispatchProps)(ContactList)
- mapStateToProps
    - state = get state from store
- mapDispatchToProps
    - dispatch = trigger action, make action, change state of store
- ContactList
    - the component (that is going to become a SMART COMPONENT / CONTAINER)

concept 3: selector

7. the root component (in REACT and REACT-REDUX)

ALL REACT APPLICATIONS HAVE ROOT COMPONENTS. IN REDUX APPLICATIONS, THIS COMPONENT TAKES ON MORE RESPONSIBILITY

HOW THEY ALL WORK TOGETHER (in flowchart)

chart