UNPKG

@wordpress/compose

Version:
40 lines (33 loc) 1.08 kB
import { createElement } from "@wordpress/element"; /** * WordPress dependencies */ import isShallowEqual from '@wordpress/is-shallow-equal'; import { Component } from '@wordpress/element'; /** * Internal dependencies */ import createHigherOrderComponent from '../../utils/create-higher-order-component'; // eslint-disable-next-line no-duplicate-imports /** * Given a component returns the enhanced component augmented with a component * only rerendering when its props/state change */ const pure = createHigherOrderComponent(Wrapped => { if (Wrapped.prototype instanceof Component) { return class extends Wrapped { shouldComponentUpdate(nextProps, nextState) { return !isShallowEqual(nextProps, this.props) || !isShallowEqual(nextState, this.state); } }; } return class extends Component { shouldComponentUpdate(nextProps) { return !isShallowEqual(nextProps, this.props); } render() { return createElement(Wrapped, this.props); } }; }, 'pure'); export default pure; //# sourceMappingURL=index.js.map