【发布时间】:2016-08-16 08:56:33
【问题描述】:
我已经看到了将 action 和 reducer 分开的论点,因为它们具有多对多的关系。
我认为这实际上并不适用于 Redux。因为只有 1 个数据存储,所以对 reducer 的操作应该是一对多的。
通常,reducer 适用于特定数据存储的特定更改。
MY_ACTION = "MY_ACTION"
function reducer(state, action) {
switch(action.type) {
case MY_ACTION: // stuff with my action to create new state
default: return state
}
}
我们可以将多个reducer 与combineReducers 组合在一起,所以为什么不使用动作本身定义动作的处理程序。
例如
class Action {
constructor(type) {
this.type = type
this.handlers = []
}
add_handler(handler) {
this.handlers += handler
}
get_reducer() {
reducer = combineReducers(this.handlers)
return (state, action) => {
if(action.type == this.type) {
return reducer(state, action)
}
return state
}
}
}
使用“鸭子”模式,我们最终将主减速器与动作声明放在同一个模块中。
是否有任何理由将 reducers + action 与 redux 分开?
【问题讨论】:
-
我不知道你在问什么或说明什么。
标签: redux