创建单独的 reducer 并将它们组合起来:
function normalizeData(data, initialValue = [], idKey = 'id') {
return data.reduce(
(accumulator, currentValue) => ({
...accumulator,
[currentValue[idKey]]: currentValue,
}),
{},
);
}
function mapIds(data, initialValue = [], idKey = 'id') {
const ids = data.map(eachData => eachData[idKey]);
return [...initialValue, ids];
}
function posts(state = {}, action) {
switch (action.type) {
case types.FETCH_POSTS_SUCCESS:
return {
byId: normalizeData(action.payload.data, state.byId),
allIds: mapIds(action.payload.data, state.allIds),
};
default:
return state;
}
}
function users(state = {}, action) {
switch (action.type) {
case types.FETCH_USERS_SUCCESS:
return {
byId: normalizeData(action.payload.data, state.byId),
allIds: mapIds(action.payload.data, state.allIds),
};
default:
return state;
}
}
export default combineReducers({ posts, users });
您还可以为这些创建辅助包装器,这些包装器可以轻松地用于创建其他实体化简器:
const byIdReducerCreator = (actionType, idKey = 'id') => (state = {}, action) => {
switch (action.type) {
case types[actionType]:
return normalizeData(action.payload.data, state, idKey);
default:
return state;
}
};
const allIdReducerCreator = (actionType, idKey = 'id') => (state = [], action) => {
switch (action.type) {
case types[actionType]:
return mapIds(action.payload.data, state, idKey);
default:
return state;
}
};
const posts = combineReducers({
byId: byIdReducerCreator('FETCH_POSTS_SUCCESS'),
allIds: allIdReducerCreator('FETCH_POSTS_SUCCESS'),
});
const users = combineReducers({
byId: byIdReducerCreator('FETCH_USERS_SUCCESS', 'someOtherId'),
allIds: allIdReducerCreator('FETCH_USERS_SUCCESS', 'someOtherId'),
});
export default combineReducers({ posts, users });