deep equal on shouldComponentUpdate

This commit is contained in:
dzannotti 2015-08-08 00:49:13 +01:00
parent 0055b0263d
commit 37a8fc190f
2 changed files with 28 additions and 0 deletions

View File

@ -1,3 +1,5 @@
import deepEqual from '../../../utils/deepEqual';
export default {
handleClick(e) {
e.stopPropagation();
@ -11,5 +13,9 @@ export default {
this.renderedChildren = [];
this.itemString = false;
this.needsChildNodes = true;
},
shouldComponentUpdate(nextProps, nextState) {
return !deepEqual(this.state, nextState) || !deepEqual(this.props, nextProps);
}
};

22
src/utils/deepEqual.js Normal file
View File

@ -0,0 +1,22 @@
const deepEqual = function (x, y) {
if ((typeof x == "object" && x != null) && (typeof y == "object" && y != null)) {
if (Object.keys(x).length != Object.keys(y).length) {
return false;
}
for (var prop in x) {
if (y.hasOwnProperty(prop)) {
if (! deepEqual(x[prop], y[prop])) {
return false;
}
} else {
return false;
}
}
return true;
} else if (x !== y){
return false;
}
return true;
}
export default deepEqual;