目标:
根据一些滤镜更新我最喜欢的颜色。
设置:
有两个减速器:filters
和colors
。
export function filters(state = {
noRed: false,
noBlack: false,
}, action) { ... })
export function colors(state = {
red: true,
black: true,
blue: true
}, action) { ... })
我想基于filters
更新colors
。我可以同时分派setFilter
和setColor
,但这不起作用,因为setColor
应该只在filters
完全减少之后才发生。我也可以考虑将filters
和colors
组合在一起,但我希望将它们分开。
实现我的目标的最好方法是什么?我真正需要的是以下内容:
export function colors(state = [], action) {
switch (action.type) {
case constants.SET_FILTER:
// assume I can access filters reducer here
var filters = filtersReducer
var colors = applyFilters(state.colors, filters)
return colors
case constants.SET_COLOR:
return action.colors
default:
return state
}
}
或者我可以在filters
reducer内部调度一个操作,但这是一个反模式。
1条答案
按热度按时间f5emj3cl1#
在slice reducers之间共享数据官方文档解释得很清楚。
SET_COLOR_BY_FILTER
操作将首先设置滤镜状态,然后使用下一个滤镜状态来设置颜色。输出: