redux-blocks
This is a work in progress and is in very early stages of development
redux-blocks
is a library for isolating and combining redux modules (referred to here on out as blocks).
It makes writing reusable redux code easier and more flexible.
Installation
npm install --save redux-blocks
Examples and tests
If you know Redux then the simplest way to understand redux-blocks is to look at the tests for mount and merge.
You can also look at the real world example application code here .
Usage
; ; ; const mounted = selectors actions types reducer ; const mergeMoreReduxCodemounted;
What are redux blocks?
Redux blocks are objects with the following syntax:
selectors: {} actions: {} types: {} {}
A simple example of an api request block looks like this:
const RequestBlock = types: REQUEST:"REQUEST" SUCCESS:"SUCCESS" selectors: get: statestateentitiesid actions: type: "REQUEST" id type:"SUCCESS" entity { }
The redux-blocks
library provides two methods to isolate and combine blocks - mount
and merge
.
Mounting blocks
Calling mount('point')(block)
will isolate selectors and reducers to a section of
the state under point (I.E. the reducer's state
will actually be the contents in state.point
).
Type strings will have a "POINT"
prefix added to it, and any action that returns a type in types will
have the appropriate type associated with it.
If we take the previous example of a simple request block, the result of mounting
it at "point"
will look like this:
const mounted = RequestBlock; mountedtypesREQUEST; // "POINT_REQUEST" mountedactions; // {type:"POINT_REQUEST",id:5} mountedactions; // {type:"POINT_SUCCESS"...} //state now looks like this: mounted; // {point:{entities:{}}} mountedselectors
Merging blocks
calling merge(block1,block2)
will merge blocks into a single block whose actions, selectors and types are a
combination of both modules selectors, actions and types.
The merged block's reducer is a new function that calls each reducer in turn (I.E. it composes reducers)
Working with redux-saga
redux-blocks
provides support for redux-saga
. Instead of importing redux-blocks
import redux-blocks/saga
.
Once imported, merge will make sure to combine the saga
property in every block.
Mounting a block with redux-saga makes sure that any put
action overrides the type with the mounted one and
any take
action correctly registered for the mounted type.