Created
October 17, 2017 16:45
-
-
Save nattatorn-dev/180ccaea0dff8c464ec574731444f095 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 { is } from "immutable"; | |
function CompareFn(objA, objB) { | |
if (objA === objB || is(objA, objB)) { | |
return true; | |
} | |
if ( | |
typeof objA !== "object" || | |
objA === null || | |
typeof objB !== "object" || | |
objB === null | |
) { | |
return false; | |
} | |
let keysA = Object.keys(objA || {}); | |
let keysB = Object.keys(objB || {}); | |
if (keysA.length !== keysB.length) { | |
return false; | |
} | |
let bHasOwnProperty = Object.prototype.hasOwnProperty.bind(objB); | |
for (let i = 0; i < keysA.length; i++) { | |
if (!bHasOwnProperty(keysA[i]) || !is(objA[keysA[i]], objB[keysA[i]])) { | |
return false; | |
} | |
} | |
return true; | |
} | |
export function deepCompare(_self, nextProps, nextState) { | |
return ( | |
!CompareFn(_self.props, nextProps) || !CompareFn(_self.state, nextState) | |
); | |
} | |
function shouldComponentUpdate(nextProps, nextState) { | |
return deepCompare(this, nextProps, nextState); | |
} | |
function puerRenderDecorator(component) { | |
component.prototype.shouldComponentUpdate = shouldComponentUpdate; | |
} | |
export default puerRenderDecorator; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment