Sha256: 951b8b2918326e8841ec53e7708e60aebd229f1fc9d2cdf82f1968a13e21198a
Contents?: true
Size: 1.03 KB
Versions: 69
Compression:
Stored size: 1.03 KB
Contents
import { createElement } from 'preact'; import { shallowDiffers } from './util'; /** * Memoize a component, so that it only updates when the props actually have * changed. This was previously known as `React.pure`. * @param {import('./internal').FunctionComponent} c functional component * @param {(prev: object, next: object) => boolean} [comparer] Custom equality function * @returns {import('./internal').FunctionComponent} */ export function memo(c, comparer) { function shouldUpdate(nextProps) { let ref = this.props.ref; let updateRef = ref == nextProps.ref; if (!updateRef && ref) { ref.call ? ref(null) : (ref.current = null); } if (!comparer) { return shallowDiffers(this.props, nextProps); } return !comparer(this.props, nextProps) || !updateRef; } function Memoed(props) { this.shouldComponentUpdate = shouldUpdate; return createElement(c, props); } Memoed.displayName = 'Memo(' + (c.displayName || c.name) + ')'; Memoed.prototype.isReactComponent = true; Memoed._forwarded = true; return Memoed; }
Version data entries
69 entries across 69 versions & 1 rubygems