Last active
November 28, 2018 09:33
-
-
Save leohxj/d43841ca6e0808e55308685a59392984 to your computer and use it in GitHub Desktop.
#react
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
function deepEquals(a, b, ca = [], cb = []) { | |
// Partially extracted from node-deeper and adapted to exclude comparison | |
// checks for functions. | |
// https://github.com/othiym23/node-deeper | |
if (a === b) { | |
return true; | |
} else if (typeof a === "function" || typeof b === "function") { | |
// Assume all functions are equivalent | |
// see https://github.com/mozilla-services/react-jsonschema-form/issues/255 | |
return true; | |
} else if (typeof a !== "object" || typeof b !== "object") { | |
return false; | |
} else if (a === null || b === null) { | |
return false; | |
} else if (a instanceof Date && b instanceof Date) { | |
return a.getTime() === b.getTime(); | |
} else if (a instanceof RegExp && b instanceof RegExp) { | |
return ( | |
a.source === b.source && | |
a.global === b.global && | |
a.multiline === b.multiline && | |
a.lastIndex === b.lastIndex && | |
a.ignoreCase === b.ignoreCase | |
); | |
} else if (isArguments(a) || isArguments(b)) { | |
if (!(isArguments(a) && isArguments(b))) { | |
return false; | |
} | |
let slice = Array.prototype.slice; | |
return deepEquals(slice.call(a), slice.call(b), ca, cb); | |
} else { | |
if (a.constructor !== b.constructor) { | |
return false; | |
} | |
let ka = Object.keys(a); | |
let kb = Object.keys(b); | |
// don't bother with stack acrobatics if there's nothing there | |
if (ka.length === 0 && kb.length === 0) { | |
return true; | |
} | |
if (ka.length !== kb.length) { | |
return false; | |
} | |
let cal = ca.length; | |
while (cal--) { | |
if (ca[cal] === a) { | |
return cb[cal] === b; | |
} | |
} | |
ca.push(a); | |
cb.push(b); | |
ka.sort(); | |
kb.sort(); | |
for (var j = ka.length - 1; j >= 0; j--) { | |
if (ka[j] !== kb[j]) { | |
return false; | |
} | |
} | |
let key; | |
for (let k = ka.length - 1; k >= 0; k--) { | |
key = ka[k]; | |
if (!deepEquals(a[key], b[key], ca, cb)) { | |
return false; | |
} | |
} | |
ca.pop(); | |
cb.pop(); | |
return true; | |
} | |
} |
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
export function shouldRender(comp, nextProps, nextState) { | |
const { props, state } = comp; | |
return !deepEquals(props, nextProps) || !deepEquals(state, nextState); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment