我不明白reduce-reducers是用来做什么的。如果我有两个包含相同动作的reducer函数,应该使用它吗?
function reducerA(state, action){
switch(action.type):
...
case 'SAME_ACTION': {...state, field: state.field+1}
}
function reducerB(state, action){
switch(action.type):
...
case 'SAME_ACTION': {...state, field: state.field*2}
}因此,如果我在reducerA和reducerB上调用reduceReducer,并为{field: 0}调用操作'SAME_ACTION‘,那么我将有一个下一个状态{field: 2}
此外,在我看来,它似乎是一种串联的reducers (意思是将它们合并到一个键下)。
我说的对吗?或者reduceReducer服务于不同的目的?
发布于 2017-06-05 22:24:40
区别在于:
combineReducers创建嵌套状态statereduceReducers creates flat考虑下面的减法。没有动作类型可以让事情变得更简单:
// this reducer adds a payload to state.sum
// and tracks total number of operations
function reducerAdd(state, payload) {
if (!state) state = { sum: 0, totalOperations: 0 }
if (!payload) return state
return {
...state,
sum: state.sum + payload,
totalOperations: state.totalOperations + 1
}
}
// this reducer multiplies state.product by payload
// and tracks total number of operations
function reducerMult(state, payload) {
if (!state) state = { product: 1, totalOperations: 0 }
if (!payload) return state
// `product` might be undefined because of
// small caveat in `reduceReducers`, see below
const prev = state.product || 1
return {
...state,
product: prev * payload,
totalOperations: state.totalOperations + 1
}
}combineReducers
每个reducer都会获得一个独立的状态(另请参阅http://redux.js.org/docs/api/combineReducers.html):
const rootReducer = combineReducers({
add: reducerAdd,
mult: reducerMult
})
const initialState = rootReducer(undefined)
/*
* {
* add: { sum: 0, totalOperations: 0 },
* mult: { product: 1, totalOperations: 0 },
* }
*/
const first = rootReducer(initialState, 4)
/*
* {
* add: { sum: 4, totalOperations: 1 },
* mult: { product: 4, totalOperations: 1 },
* }
*/
// This isn't interesting, let's look at second call...
const second = rootReducer(first, 4)
/*
* {
* add: { sum: 8, totalOperations: 2 },
* mult: { product: 16, totalOperations: 2 },
* }
*/
// Now it's obvious, that both reducers get their own
// piece of state to work withreduceReducers
所有reducers 共享相同状态的
const addAndMult = reduceReducers(reducerAdd, reducerMult)
const initial = addAndMult(undefined)
/*
* {
* sum: 0,
* totalOperations: 0
* }
*
* First, reducerAdd is called, which gives us initial state { sum: 0 }
* Second, reducerMult is called, which doesn't have payload, so it
* just returns state unchanged.
* That's why there isn't any `product` prop.
*/
const next = addAndMult(initial, 4)
/*
* {
* sum: 4,
* product: 4,
* totalOperations: 2
* }
*
* First, reducerAdd is called, which changes `sum` = 0 + 4 = 4
* Second, reducerMult is called, which changes `product` = 1 * 4 = 4
* Both reducers modify `totalOperations`
*/
const final = addAndMult(next, 4)
/*
* {
* sum: 8,
* product: 16,
* totalOperations: 4
* }
*/用例
combineReducers -每个reducer管理自己的状态切片(例如state.todos和state.logging)。这在创建根reducer.reduceReducers时非常有用-每个reducer管理相同的状态。当链接几个应该在相同状态下运行的减速器时,这很有用(例如,当组合使用redux-actions)中的handleAction创建的几个减速器时,可能会发生这种情况
与最终状态形状的区别是显而易见的。
发布于 2018-10-21 21:37:58
我也不明白reduce-reducers想要解决什么问题。@Tomáš描述的用例可以通过一个简单的Reducer来实现。毕竟,Reducer只是一个接受app-state和操作的函数,并返回一个包含新app-state的对象。例如,您可以执行以下操作,而不是使用redux提供的combineReducers:
import combinationReducer from "./combinationReducer";
import endOfPlayReducer from "./endOfPlayReducer";
import feedbackReducer from "./feedbackReducer";
function combineReducers(appState, action) {
return {
combination: combinationReducer(appState, action),
feedbacks: feedbackReducer(appState, action),
endOfPlay: endOfPlayReducer(appState, action)
};
}当然,在这里,你的缩减程序接受整个应用程序状态,只返回他们负责的部分。同样,这只是一个函数,您可以根据自己的喜好对其进行自定义。你可以阅读更多关于它的here
https://stackoverflow.com/questions/38652789
复制相似问题