Skip to content

Instantly share code, notes, and snippets.

@kishanio
Last active July 1, 2018 08:34
Show Gist options
  • Save kishanio/e6cc0cef2ccd5425ca938768b9464e2e to your computer and use it in GitHub Desktop.
Save kishanio/e6cc0cef2ccd5425ca938768b9464e2e to your computer and use it in GitHub Desktop.
Redux : Manually implementing Combine Reducer
// Reference : https://egghead.io/lessons/javascript-redux-implementing-combinereducers-from-scratch
const combineReducers = (reducers) => {
return ( state = {}, action) => {
const keys = Object.keys(reducers);
const arr = keys.reduce((accumulator, key) => {
// since accumulator is created inside reducer function we don't have to worry about if it's mutable or not.
// reducer still stays as a pure function.
accumulator[key] = reducers[key](state[key],action);
return accumulator;
}, {});
return arr;
};
}
// functions can be combined
const combineReducers = (reducers) => {
return ( state = {}, action) => {
return Object.keys(reducers).reduce((accumulator, key) =>
accumulator[key] = reducers[key](state[key],action);
return accumulator;
}, {});
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment