我使用immutable.JS通过redux-immutablejs管理我的商店。我现在想使用redux-form库,但我遇到了一个组合reducer的问题。
Redux-immutable提供了combineReducers函数,该函数将检查传递的所有reducers是否返回不可变对象。
Redux本身提供了combineReducers函数,不执行此类检查。
Redux-form要求你包含他们的reducer但是我不能使用Redux immutable的combineReducers,因为它会失败。
所以我要做的就是基本上将这两个函数的输出结合起来:
import { combineReducers } from 'redux';
import { combineReducers as combineReducersUtils } from 'redux-utils';
import {reducer as formReducer} from 'redux-form';
const mainReducers = combineReducersUtils({
devices, alarms
});
const extraReducers = combineReducers({
form: formReducer
});
export default (mainReducers + extraReducers);
最后一行显然不起作用,但基本上说明了我的目标。
感谢您花时间阅读本文。
答案 0 :(得分:6)
也许是这样的?
function rootReducer(state, action) {
const newState = combineReducersUtils({devices, alarms})(state, action);
return combineReducers({form: formReducer})(newState, action);
}
它应该有效,因为combineReducers的返回值只是一个reducer:
(Function):一个reducer,它调用reducers对象中的每个reducer,并构造一个具有相同形状的状态对象。
https://github.com/rackt/redux/blob/master/docs/api/combineReducers.md
更新为不在每个调度上创建新函数:
const mainReducers = combineReducersUtils({devices, alarms});
const formReducers = combineReducers({form: formReducer});
function rootReducer(state, action) {
return formReducers(mainReducers(state, action), action);
}