Flux #
Flux is a pattern consisting of a singleton dispatcher that emits events (actions) with a type
and payload
, triggering callback functions in all stores. The stores then switch
on the action type and decide if they should respond to the emitted action
Data flow is generally as follows:
- Component calls action creator function
- The action creator may make an API call and await the response
- The action creator then dispatches an event through the `Dispatcher’ singleton
- When stores are registered to the
Dispatcher
in their constructor, they register a callback function to execute on every emitted action. Thus, each store will execute its callback - If the store is responsible for the action
type
(as determined by theswitch
statement) it updates its data - A higher order component (HOC) is used to inject store state as props into connected components. Thus, whenever the store state changes so do the props of connected components, thus causing them to re-render
In general, the Dispatcher
is simply a registry for callbacks