markerikson / redux-ecosystem-links

A categorized list of Redux-related addons, libraries, and utilities
5.23k stars 372 forks source link

Added combineCrossSliceReducers. #79

Closed visusnet closed 6 years ago

visusnet commented 6 years ago

combineCrossSliceReducers is very similar to combineSectionReducers but with a small twist:

While combineSectionReducers passes the original entireState as a third parameter to each reducer, combineCrossSliceReducers passes the updated state which is the result of all previous reducers.

Example:

const rootReducer = combineSectionReducers({
    increment: (state = 0, action) => state + action.value,
    mirrorIncrement: (state = 0, action, entireState) => entireState.increment
});

rootReducer(undefined, {type: 'foobar', value: 1});
// {
//     increment: 1,
//     mirrorIncrement: 0
// }

but with combineCrossSliceReducers:

const stage1Reducers = {
    increment: (state = 0, action) => state + action.value
};
const stage2Reducers = {
    mirrorIncrement: (state = 0, action, entireState) => entireState.increment
};
const rootReducer = combineCrossSliceReducers(stage1Reducers, stage2Reducers);

rootReducer(undefined, {type: 'foobar', value: 1});
// {
//     increment: 1,
//     mirrorIncrement: 1
// }
markerikson commented 6 years ago

Neat, thanks!