redux-simplifr
v0.0.3
Published
Useful redux utilities for combining multiple reducers with using simplifr.
Downloads
6
Maintainers
Readme
redux-simplifr
Some auxiliary utilities that make simplifr and redux closer.
API Reference
combineReducers(reducers)
Takes an object of reducer arrays, returns combined reducer function
Let's show an example how it works,
...
import {simplify} from 'simplifr'
import {combineReducers} from 'redux-simplifr'
// initialize data for separated components
const initialState = {
path: {
to: {
component1: {/* JSON */},
component2: {/* JSON */}
}
}
}
// define reducers for component1
const reducer1 = (state = 0, action) => action.type === 'increment1' ? state + 1 : state
//define reducers for component2
const reducer2 = (state = [], action) => action.type === 'push2' ? [ ...state, action.value ] : state
// gather all reducers into a single object with componets separated by `path`
const reducers = {
`path.to.component1.counter1`: reducer1,
`path.to.component2.stack2`: reducer2
}
// combine reducers via simplifr
const reducer = combineReducers(reducers)
// Now we can dispatch reducer with actions
reducer(initialState, {
type: 'increment1',
path: 'path.to.component1.counter1'
})
/* the result state:
{
path: {
to: {
component1: { counter1: 1 },
component2: {}
}
}
}
*/
// if we don't define exact `path` in action, all reducers will be called
reducer(initialState, {
type: 'increment1'
})
/* the result state:
{
path: {
to: {
component1: { counter1: 1 },
component2: { stack2: [] } // is also called
}
}
}
*/
...
License
MIT