Skip to content

Instantly share code, notes, and snippets.

@laytong
Created July 12, 2016 18:33
Show Gist options
  • Save laytong/28def1826f7090efaab1c4d20a565c14 to your computer and use it in GitHub Desktop.
Save laytong/28def1826f7090efaab1c4d20a565c14 to your computer and use it in GitHub Desktop.
Remove complexity from redux reducers
const initialObjectivesState = {
aString: '',
};
/*
* As the number of actions increases, the cyclomatic complexity of the reducer will skyrocket
*/
const Reducer = function (state = initialObjectivesState, action) {
switch(action.type){
"UPDATE_STRING": function(state, action){
return {
...state,
aString: action.value,
};
};
default: return state;
}
};
export default Reducer;
const initialObjectivesState = {
aString: '',
};
const actionMap = {
"UPDATE_STRING": function(state, action){
return {
...state,
aString: action.value,
};
}
};
const Reducer = function (state = initialObjectivesState, action) {
if(actionMap[action.type]){
return actionMap[action.type](state, action);
} else {
return state;
}
};
export default Reducer;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment