Redux enhancer for composing higher order reducers
- Simple enhancer method for adding higher order reducers to redux.
- Wrap the reducer with functionality ensuring that the higher order reducers run before the primary reducer.
npm install --save redux-higher-orders
import { createStore } from 'redux'
import { applyHigherOrders } from 'redux-higher-orders'
const higherOrders = [
reducer => (state, action) => reducer(state, action),
higherOrder2,
higherOrder3
]
let store = createStore(
reducer,
initialState,
applyHigherOrders(...higherOrders)
)
Higher order reducers are a way to extend the Redux reducer with custom functionality. Higher orders let you wrap the store's internal reducer
method.
The most common use case for higher order reducers is to support modification of actions or state before/after reduction. An example is to support batching of actions
...higherOrders
(arguments): Functions that conform to the higher-order API. Each higherOrder receives thereducer
function and returns a reducer function. The higher order signature is(reducer) => (state, action) => reducer(state, action)
.
Function
A store enhancer that applies the given higher order reducers. The store enhancer signature iscreateStore => createStore'
but the easiest way to apply it is to pass it tocreateStore()
as the lastenhancer
argument.
import { createStore } from 'redux'
import { applyHigherOrders } from 'redux-higher-orders'
import todos from './reducers'
function logger() {
return (reducer) => (state, action) => {
console.log('will reduce', action)
// Reduce the next state
let resultState = reducer(state, action)
console.log('state after reduction', resultState)
return resultState
}
}
let store = createStore(
todos,
[ 'Use Redux' ],
applyHigherOrders(logger())
)
store.dispatch({
type: 'ADD_TODO',
text: 'Understand higher orders'
})