I want to show the difference between a components current and next props. For example:
componentWillReceiveProps(nextProps) {
let diff = someFunction(this.props, nextProps);
console.log(diff);
}
How is this done?
I want to show the difference between a components current and next props. For example:
componentWillReceiveProps(nextProps) {
let diff = someFunction(this.props, nextProps);
console.log(diff);
}
How is this done?
You can do a shallow comparison with a function like the following (I'm using ES6):
function compareObjects (objectA, objectB) {
if (objectA === objectB) {
return true // return true if both variables are referencing the same object
}
let key
// check what property does objectA have that objectB has not
for (key in objectA) {
if (objectA.hasOwnProperty(key) && !objectB.hasOwnProperty(key)) {
return false // return false if there's a difference
}
}
// check what property does objectB have that objectA has not
for (key in objectB) {
if (objectB.hasOwnProperty(key) && !objectA.hasOwnProperty(key)) {
return false // return false if there's a difference
}
}
}
Note that this is only shallow checking and does the comparison on first level. It only compares if the objects given consist of the same values (therefore it's called shallow).