【发布时间】:2019-07-15 22:04:02
【问题描述】:
我有两个看起来像这样的标准减速器
减速器1
const reducer1 = (state, action) => {
switch(action.type) {
case('clear'):
return {
...state,
reducer1property1: null
}
...<other cases>...
}
}
Reducer2
const reducer2 = (state, action) => {
switch(action.type) {
case('clear'):
return {
...state,
reducer2property1: null
}
...<other cases>...
}
}
因此,如果我调度一个“清除”类型的操作,那么 property1 将在两个减速器中被清除。
我这样组合减速器:
const combinedReducer = combineReducers({
Reducer1,
Reducer2
})
我的问题是:有没有办法做到这一点,而无需分别在每个减速器中编写“清晰”案例?例如,有没有办法将“清除”案例添加到combinedReducer,以便我只能写一次?在我的实际应用中,有更多的 reducer,所以我希望简化。
谢谢!
【问题讨论】:
-
什么是reducer1property1和reducer1property2?它们是减速器名称还是一些随机键?代码不再重复。这取决于reducer1property1 等。那么property1 将在两个reducer 中被清除。 - 那么它是property1 还是reducer1property1?
标签: javascript reactjs react-native redux