redux-xforms
redux-xforms
is a collection of reducer transformers (higher-order reducers). They can be used to define reducers by direct composition of simpler transformers.
Reducer transformers are similar to transducers
, but lacks the completion step. It's a special case of higher-order reducers, functions that transform a reducer to another reducer:
xform: reducer -> reducer
reducer: state, input -> state
Story
As wrote this article on the topic "Reducer composition with Higher-Order Reducers", it struck me that these Higher-Order Reducers could be decomposed into much simpler transformations. So, I started putting together a colletion, and here it is redux-xforms
!
Installation
npm install --save redux-xforms
Example usage
Setting up a byFilterName
transformer:
const hasFilterName = actionconst mapActionToFilterName = actionfilterName // we can call this later with a reducer to get the transformconst createByFilter = const createGetByFilter =
When createByFilter
is called with a reducer, the transformers will be applied one by one, from right to left.
When calling the transformed reducer with an action, it will come in from the left.
const ACTION_TYPE = 'ACTION_TYPE'const reducer = { if actiontype === ACTION_TYPE return nested: actionpayload return state} const transformedReducer = const getNestedState = // reducing actions:const state = {} type: ACTION_TYPE payload: 'some-payload' filterName: 'myFilterName' console// { myFilterName: { nested: 'some-payload' } } console// 'some-payload'
createByKey
is simply a composition of other reducer transformers.
const createByFilter = // we can transform (state, input -> state) selectors as wellconst createGetByFilter =