Skip to content

Instantly share code, notes, and snippets.

@stepankuzmin
Last active February 13, 2018 15:11
Show Gist options
  • Save stepankuzmin/11237d10f217850943d20268903e49d7 to your computer and use it in GitHub Desktop.
Save stepankuzmin/11237d10f217850943d20268903e49d7 to your computer and use it in GitHub Desktop.
shouldUpdateImmutable
import { is, isImmutable } from 'immutable';
import { shouldUpdate, shallowEqual } from 'recompose';
const shouldUpdateImmutable = shouldUpdate((props, nextProps) => {
const keys = Object.keys(props);
const nextKeys = Object.keys(nextProps);
if (keys.length !== nextKeys.length) {
return true;
}
for (let i = 0; i < keys.length; i += 1) {
const key = keys[i];
if (!hasOwnProperty.call(nextProps, key)) {
return true;
}
const prop = props[key];
const nextProp = nextProps[key];
if (isImmutable(prop) && !is(prop, nextProp)) {
return true;
}
if (!shallowEqual(prop, nextProp)) {
return true;
}
}
return false;
});
export default shouldUpdateImmutable;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment