Wrap function combineReducers
in Redux to set initial state.
Reducers should not know about the initial states.
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.
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.
npm install redux better-combine-reducers
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)
Anyone who have an idea of better module name are welcomed write one in #1!