Skip to content

Instantly share code, notes, and snippets.

@l0gicgate
Last active March 22, 2016 20:46
Show Gist options
  • Save l0gicgate/ed2b831a86d13f941a98 to your computer and use it in GitHub Desktop.
Save l0gicgate/ed2b831a86d13f941a98 to your computer and use it in GitHub Desktop.
export default class ReducerChain {
constructor(reducers) {
this.reducers = reducers;
if (!Array.isArray(reducers)) {
throw new Error('To create a reducer chain you must pass in an array of functions.');
}
return this.reducer;
}
reducer = (state, action) => {
let result = state;
for (let i = 0, len = this.reducers.length; i < len; i++) {
result = this.reducers[i](result, action);
}
return result;
};
}
import ReducerChain from './ReducerChain';
const SOME_ACTION = 'SOME_ACTION';
const SOME_OTHER_ACTION = 'SOME_OTHER_ACTION';
const initialState = {
someProp: 'someValue',
someOtherProp: 'someOtherValue',
}
function reducer1 = function (state, action) {
switch (action.type) {
case SOME_ACTION:
return Object.assign({}, state, {
someProp: 'someNewValue',
});
default:
return state;
}
}
function reducer2 = function (state, action) {
switch (action.type) {
case SOME_OTHER_ACTION:
return Object.assign({}, state, {
someOtherProp: 'someOtherNewValue',
});
default:
return state;
}
}
export default new ReducerChain([
reducer1,
reducer2,
]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment