我已经看到分离动作和缩减器的论点,因为它们有多对多的关系。为什么单独操作+ reducer在Redux中?
我不认为这实际上适用于Redux。由于只有1个数据存储区,因此对还原器的操作应该是1对多的。
典型的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
}
}
我们可以结合多个减速带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
}
}
}
随着“鸭子”的格局,我们最终把主减速器相同的模块作为行动宣言英寸
是否有任何理由让Reduce +动作与redux分开?
我不知道你在问什么或说明。 – JMM