Created
July 12, 2016 18:33
-
-
Save laytong/28def1826f7090efaab1c4d20a565c14 to your computer and use it in GitHub Desktop.
Remove complexity from redux reducers
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
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; |
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
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