Created
February 13, 2017 12:29
-
-
Save TCotton/736555e5a7347060cfb7761467e47c51 to your computer and use it in GitHub Desktop.
React deep object comparison -> used in shouldComponentUpdate
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 const isObjectEqual = (obj1, obj2) => { | |
if(!isObject(obj1) || !isObject(obj2)) { | |
return false; | |
} | |
if (obj1 === obj2) { | |
return true; | |
} | |
const item1Keys = Object.keys(obj1).sort(); | |
const item2Keys = Object.keys(obj2).sort(); | |
if (!isArrayEqual(item1Keys, item2Keys)) { | |
return false; | |
} | |
return item2Keys.every(key => { | |
const value = obj1[key]; | |
const nextValue = obj2[key]; | |
if (value === nextValue) { | |
return true; | |
} | |
return Array.isArray(value) && | |
Array.isArray(nextValue) && | |
isArrayEqual(value, nextValue); | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi. Where is
isObject
defined?