我有多个减速器,每个减速器都有一个类型“INIT”。我想要实现的是,从触发动作的位置来看,只有相关的减速器才能接收该动作。有没有中间件可以做到这一点?
您在创建减速器时是否重复使用减速器逻辑?
你可以尝试这样的事情:-
function createCounterWithNamedType(counterName = '') {
return function counter(state = 0, action) {
switch (action.type) {
case `INCREMENT_${counterName}`:
return state + 1;
case `DECREMENT_${counterName}`:
return state - 1;
default:
return state;
}
}
}
const rootReducer = combineReducers({
counterA : createCounterWithNamedType('A'),
counterB : createCounterWithNamedType('B'),
counterC : createCounterWithNamedType('C'),
});
store.dispatch({type : 'INCREMENT_B'});
console.log(store.getState());
这就是所谓的高阶减速器,您可以通过创建包装函数来使用相同的减速器逻辑,如上面的代码所示。
您可以查看有关高阶减速器的更多信息here https://redux.js.org/docs/recipes/reducers/ReusingReducerLogic.html
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)