Created
February 1, 2017 01:37
-
-
Save jamesgarfield/c068058274a649d624129421106fcb16 to your computer and use it in GitHub Desktop.
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
import _ from 'lodash'; | |
export function replacementReducer(actionType, defaultVal) { | |
if (defaultVal === undefined) { | |
throw new Error( | |
`No default value passed to replacementReducer for action type ${actionType}` | |
); | |
} | |
return (state = defaultVal, action = {}) => { | |
if (action.type === actionType) { | |
let newValue = action.payload; | |
let oldValue = state; | |
if (_.isEqual(newValue, oldValue)) { | |
// don't kill off shallow comparison of array objects if they are equal | |
return state; | |
} | |
return action.payload; | |
} | |
return state; | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment