Using Pure Components

Pure Components do shallow equality checks in shouldComponentUpdate by default. This is intended to prevent renders when neither props nor state have changed.

Recompose offers a Higher Order Component called pure for this purpose and React added React.PureComponent in v15.3.0.

Bad

  1. export default (props, context) => {
  2. // ... do expensive compute on props ...
  3. return <SomeComponent {...props} />
  4. }

Good

  1. import { pure } from 'recompose';
  2. // This won't be called when the props DONT change
  3. export default pure((props, context) => {
  4. // ... do expensive compute on props ...
  5. return <SomeComponent someProp={props.someProp}/>
  6. })

Better

  1. // This is better mainly because it uses no external dependencies.
  2. import { PureComponent } from 'react';
  3. export default class Example extends PureComponent {
  4. // This won't re-render when the props DONT change
  5. render() {
  6. // ... do expensive compute on props ...
  7. return <SomeComponent someProp={props.someProp}/>
  8. }
  9. }
  10. })