shallow-utils
Utilities for shallow comparisons, particularly for React optimisation
Installation
Install the package with npm:
npm install shallow-utils
Usage
Use the shallow comparison as an auto-typing wrapper for shallow-equal
's shallowEqualArray
and shallowEqualObject
.
let a = title: 'The Wizard of Oz'let b = title: 'The Wizard of Oz' console// true let c = 5let d = 5console// true
When you want to compare an object minus a set of attributes, use shallowEqualExcept
.
Then, for debugging purposes, use shallowItemsDifferExcept
as a helper to let you know what
changed.
let a = title: 'The Wizard of Oz' showing: falselet b = title: 'The Wizard of Oz' showing: true console// false console// true btitle = 'The Matrix'console// ['title',]
All together in one shouldComponentUpdate
:
Component { if ! // console.log('arrayOfStuff changed') return true let checkedProps = 'arrayOfStuff' if ! // console.log('misc props changed', shallowItemsDifferExcept(this.props, nextProps, checkedProps)) return true return false }