react-should-component-update
TypeScript icon, indicating that this package has built-in type declarations

1.1.2 • Public • Published

React Should Component Update

Higher-Order Component for adding shouldComponentUpdate to components. It supports easy to set shortcuts.

Install

npm install --save react-should-component-update

Usage

import shouldComponentUpdate from 'react-should-component-update';
import MyComponent from './my_component';
 
// Path shortcut
// `shouldComponentUpdate ( path )( Component )`
// Updates the component if the prop at `path` changed
 
shouldComponentUpdate ( 'myprop.foo' )( MyComponent );
 
// Path toggle shortcut
// `shouldComponentUpdate ( [path, valuePath] )( Component )`
// Updates the component if the prop at `path` becomes equal-to/different-from the prop at `valuePath`
// This is useful when your component should be re-rendered only when it gets selected/unselected
 
shouldComponentUpdate ( ['myprop.foo', 'foo'] )( MyComponent );
 
// Plain function
// `shouldComponentUpdate ( myFn )( Component )`
// Updates the component if `myFn` returns true
 
function myFn ( props, nextProps ) {
  // Update logic here
}
 
shouldComponentUpdate ( myFn )( MyComponent );
 
// Boolean shortcut
// `shouldComponentUpdate ( bool )( Component )`
 
shouldComponentUpdate ( false )( MyComponent );
 
// Multiple rules
// Updates the component if at least one updating rule passes
 
shouldComponentUpdate ( 'myprop.bar', ['myprop.foo', 'foo'], myFn )( MyComponent );

Related

License

MIT © Fabio Spampinato

Package Sidebar

Install

npm i react-should-component-update

Weekly Downloads

8

Version

1.1.2

License

MIT

Unpacked Size

13.2 kB

Total Files

8

Last publish

Collaborators

  • fabiospampinato