Skip to content

Instantly share code, notes, and snippets.

@joeljameswatson
Created September 25, 2018 03:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joeljameswatson/876005477b3033604c46a8a85d1a6b81 to your computer and use it in GitHub Desktop.
Save joeljameswatson/876005477b3033604c46a8a85d1a6b81 to your computer and use it in GitHub Desktop.
redux combineReducers function implementation
const combineReducers = reducers => {
return (state = {}, action) => {
// Reduce all the keys for reducers from `todos` and `visibilityFilter`
return Object.keys(reducers).reduce(
(nextState, key) => {
// Call the corresponding reducer function for a given key
nextState[key] = reducers[key] (
state[key],
action
);
return nextState;
},
{} // The `reduce` on our keys gradually fills this empty object until it is returned.
);
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment