Skip to content

Instantly share code, notes, and snippets.

@aegyed91
Created April 18, 2016 13:22
Show Gist options
  • Save aegyed91/020a4738b9840f1f571f1abdd6deb653 to your computer and use it in GitHub Desktop.
Save aegyed91/020a4738b9840f1f571f1abdd6deb653 to your computer and use it in GitHub Desktop.
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