Last active
July 1, 2018 08:34
-
-
Save kishanio/e6cc0cef2ccd5425ca938768b9464e2e to your computer and use it in GitHub Desktop.
Redux : Manually implementing Combine Reducer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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