Created
May 13, 2016 08:26
-
-
Save amoilanen/67274bc7b02bdde72e305d1e86a2d86a to your computer and use it in GitHub Desktop.
'combineReducers' from Redux re-implemented
This file contains hidden or 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
/* | |
* Demonstrates how 'combineReducers' can be implemented. | |
*/ | |
function combineReducers(reducers) { | |
return function(state, action) { | |
var reducerNames = Object.keys(reducers); | |
var combination = {}; | |
reducerNames.forEach(function(reducerName) { | |
combination[reducerName] = reducers[reducerName](state[reducerName], action); | |
}); | |
return combination; | |
}; | |
} | |
/* | |
* Client code | |
*/ | |
var state = { | |
key1: { | |
subkey1: 'value1' | |
}, | |
key2: 'value2' | |
}; | |
var action = 'testAction'; | |
function func1(state, action) { | |
return state.subkey1 + '_reduced'; | |
} | |
function key2(state, action) { | |
return state + '_reduced'; | |
} | |
var reducer = combineReducers({ | |
key1: func1, key2 | |
}); | |
console.log(reducer(state, action)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment