Skip to content

Instantly share code, notes, and snippets.

@jamesgarfield
Created February 1, 2017 01:37
Show Gist options
  • Save jamesgarfield/c068058274a649d624129421106fcb16 to your computer and use it in GitHub Desktop.
Save jamesgarfield/c068058274a649d624129421106fcb16 to your computer and use it in GitHub Desktop.
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