Connect Redux and Effects-as-data
This module contains Redux cmds and handlers for a seamless Redux and effects-as-data connection. It also exposes helper functions to autogenerate cmds and effects-as-data functions from Redux actions.
This module is meant to be used with effects-as-data.
Installation
npm i effects-as-data-redux --save
How it works
Data flow
- Redux passes state to React using
react-redux
. - React handles UI events and calls effects-as-data functions that have been passed in as props.
- Effect-as-data dispatches Redux actions.
Using effects-as-data to control Redux further decouples the view layer from Redux and gives you the full power of effects-as-data to execute business logic and dispatch Redux actions at the appropriate times. This breaks the (IMHO) bad habit of using the Redux dispatch cycle as a general purpose message bus for the application and instead allows Redux to be used only for dispatching actions for state mutation.
Usage
Using commands
// actions.js - Redux actionsconst setUser = { type: 'SET_USER' payload }
// cmds.js const reduxCmds = // combine and export universal cmds and the redux cmds generated above state: reduxCmds ...cmds
// sample effects-as-data function { // the httpGet cmds comes from effects-as-data-universal const user = cmds // this will dispatch a SET_USER action to the Redux store cmdsstate}
Setting up handlers
// export a function that takes a Redux store and builds all the handers ... handlers
Generating effects-as-data functions
// It is not necessary to generate these functions but is convenient// so that your application can talk to Redux indirectly through// effects-as-data. If your view only talks to effects-as-data,// and effects-as-data talk to Redux, you'll have a clean unidirectional// data-flow through the application.const reduxFunctions = // Combine functions from project and reduxFunctions generated aboveconst allFunctions = ...functions ...reduxFunctions const config = onCommandComplete: consolelog //for telemetry // create the redux store and pass it to the handlersconst store = const handlers = // This will export an object of promise return functions// that have been generated from the effects-as-data functions// represented by allFunctionsconfig handlers allFunctions