Skip to content

Instantly share code, notes, and snippets.

@adamgruber
Created September 6, 2017 14:14
Show Gist options
  • Save adamgruber/db0831ba7e9e0dfaceac5c73397be7fd to your computer and use it in GitHub Desktop.
Save adamgruber/db0831ba7e9e0dfaceac5c73397be7fd to your computer and use it in GitHub Desktop.
HOC to turn a stateless functional component into an instance of a PureComponent
import { PureComponent } from 'react';
/**
* PureComponent HOC.
*
* @param {Function} renderFn Stateless functional component
* @return {PureComponent} Component wrapped as PureComponent
*/
module.exports = function asPureComponent(renderFn) {
const comp = class AsPureComponent extends PureComponent {
render() {
return renderFn(this.props);
}
};
comp.displayName = `${renderFn.name}AsPureComponent`;
return comp;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment