Created
December 11, 2015 11:27
-
-
Save ssnau/00271a5fb070119a7df6 to your computer and use it in GitHub Desktop.
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 pure(Clazz) { | |
Clazz.prototype.shouldComponentUpdate = function scu(nextProps, nextState) { | |
return shallowCompare(this, nextProps, nextState); | |
} | |
// 包装一层 | |
clazz Wrapper extends React.Component { | |
render() { | |
var props = {}; | |
each(this.props, function (key, val) { | |
props[key] = val; | |
if (isCursor(val)) props['_cursor_val_' + key] = val(); | |
}); | |
return ( | |
<Clazz {...props} > | |
); | |
} | |
} | |
return Wrapper; | |
} | |
function noop(){} | |
function isCursor(val) { | |
return typeof val === 'function' && val.get && val.update; | |
} | |
function shallowCompare(instance, nextProps, nextState) { | |
return ( | |
!shallowEqual(instance.props, nextProps) || | |
!shallowEqual(instance.state, nextState) | |
); | |
} | |
/** | |
* Performs equality by iterating through keys on an object and returning | |
* false when any key has values which are not strictly equal between | |
* objA and objB. Returns true when the values of all keys are strictly equal. | |
* | |
* @return {boolean} | |
*/ | |
function shallowEqual(objA, objB) { | |
if (objA === objB) { | |
return true; | |
} | |
if (typeof objA !== 'object' || objA === null || | |
typeof objB !== 'object' || objB === null) { | |
return false; | |
} | |
var keysA = Object.keys(objA); | |
var keysB = Object.keys(objB); | |
if (keysA.length !== keysB.length) { | |
return false; | |
} | |
// Test for A's keys different from B. | |
var bHasOwnProperty = Object.prototype.hasOwnProperty.bind(objB); | |
for (var i = 0; i < keysA.length; i++) { | |
if (!bHasOwnProperty(keysA[i]) || !equal(objA[keysA[i]], objB[keysA[i]])) { | |
return false; | |
} | |
} | |
return true; | |
} | |
function equal(a, b) { | |
return a === b; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment