Created
April 18, 2016 13:22
-
-
Save aegyed91/020a4738b9840f1f571f1abdd6deb653 to your computer and use it in GitHub Desktop.
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
function person(state, action) { | |
state = state || {}; | |
switch(action.type){ | |
case 'ADD_INFO': | |
return Object.assign({}, state, action.payload); | |
default: | |
return state; | |
} | |
} | |
function hoursWorked(state, action) { | |
state = state || 0; | |
switch(action.type){ | |
case 'ADD_HOUR': | |
return state + 1; | |
case 'SUBTRACT_HOUR': | |
return state - 1; | |
default: | |
return state; | |
} | |
} | |
var myReducers = { | |
person: person, | |
hoursWorked: hoursWorked | |
}; | |
function combineReducers(reducers) { | |
return function(state, action) { | |
state = state || {}; | |
return Object.keys(reducers).reduce(function(nextState, key) { | |
nextState[key] = reducers[key](state[key], action); | |
return nextState; | |
}, {}); | |
}; | |
} | |
var rootReducer = combineReducers(myReducers); | |
var firstState = rootReducer(undefined, {type: 'ADD_INFO', payload: {name: 'Brian'}}); | |
var secondState = rootReducer({hoursWorked: 10, person: {name: 'Joe'}}, {type: 'ADD_HOUR'}); | |
console.log('***FIRST STATE***:', firstState); | |
console.log('***SECOND STATE***:', secondState); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment