reactjs - React - how to show the difference between the current and next props? -
i want show difference between components current , next props. example:
componentwillreceiveprops(nextprops) { let diff = somefunction(this.props, nextprops); console.log(diff); } how done?
you can shallow comparison function following (i'm using es6):
function compareobjects (objecta, objectb) { if (objecta === objectb) { return true // return true if both variables referencing same object } let key // check property objecta have objectb has not (key in objecta) { if (objecta.hasownproperty(key) && !objectb.hasownproperty(key)) { return false // return false if there's difference } } // check property objectb have objecta has not (key in objectb) { if (objectb.hasownproperty(key) && !objecta.hasownproperty(key)) { return false // return false if there's difference } } } note shallow checking , comparison on first level. compares if objects given consist of same values (therefore it's called shallow).
Comments
Post a Comment