我有一个形状像这样的商店:
{
// ...data
user: {
warranties: {
W_1: ['O_1', 'O_2'],
W_2: ['O_3', 'O_4']
}
}
}
以 开头的键W_
是保修,以 开头的键O_
是选项。
对于每个保修,我都有一个或多个与之相关联的选项,其中的关系采用user.warranties
以下形式:warranty => [options]
.
为了实现它,我正在像这样组合我的减速器:
rootReducer = combineReducers({
// ...other main reducers
user: combineReducers({
// ...other user reducers
warranties
})
})
现在,“问题”是USER_WARRANTY
和USER_OPTION
动作都由同一个reducer处理,因为:
添加选项时,我需要将其推送到正确的保修条目。
相反,当我添加保修时,我需要使用其默认选项填充它。
最终,它们对同一片数据进行操作
因此,warranties
reducer 必须对这两个动作做出反应,如下所示:
export default function warranties(state = {}, action) {
switch (action.type) {
case USER_WARRANTIES_ADD:
// add warranty key to `user.warranties`
case USER_WARRANTIES_REMOVE:
// remove warranty key from `user.warranties`
case USER_OPTIONS_ADD:
// push option to `user.warranties[warrantyID]`
case USER_OPTIONS_REMOVE:
// remove option from `user.warranties[warrantyID]`
default:
return state
}
}
我想把它分成两个减速器,warranties
和options
,但仍然让它们在同一个数据片上运行。
理想情况下,我会像这样组成我的根减速器:
rootReducer = combineReducers({
// ...other main reducers
user: combineReducers({
// ...other user reducers
warranties: magicalCombine({
warranties,
options
})
})
})
magicalCombine
我很难找到的功能在哪里。
我试过reduce-reducers
了,但看起来第二个减速器(options
)实际上从未到达过,而且我实际上不确定它,因为我不是试图达到平坦状态,而是实际上在同一个键上操作。