better-combine-reducers
v1.0.3
Published
Better redux combineReducers, initial state injectable.
Downloads
10
Readme
better-combine-reducers
Wrap function combineReducers
in Redux to set initial state.
Concept
Reducers should not know about the initial states.
Redux's reducers know about the initial state
When creating a reducer, we set default states as its default argument.
function reducer(state = defaultState, action) {
}
Though it's the recommended way, (See http://redux.js.org/docs/basics/Reducers.html ), we often meet some cases when it doesn't go well.
Case: restarting an app
Consider an app that caches the last state and uses it as an initial state when the app restarts.
This means that initial states vary and reducers should be independent from them. However, current spec of combineReducers
forces every reducer to return initial state when undefined is given. How should we tell them the initial state we want to set?
This library combines reducers with initial state.
Installation
npm install redux better-combine-reducers
Usage
import { combineReducers } from 'redux'
import better from 'better-combine-reducers'
const reducerA = (state, action) => { counter: state.counter + 1 }
const reducerB = (state, action) => { counter: state.counter + 1 }
const initialState = {
A: { counter: 100 },
B: { counter: 100 },
}
const reducer = better(combineReducers)({ A: reducerA, B: reducerB }, initialState)
Better Module Names?
Anyone who have an idea of better module name are welcomed write one in #1!