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

Version Path
isomorfeus-preact-10.6.50 node_modules/preact/compat/src/memo.js
isomorfeus-preact-10.6.49 node_modules/preact/compat/src/memo.js
isomorfeus-preact-10.6.48 node_modules/preact/compat/src/memo.js
isomorfeus-preact-10.6.47 node_modules/preact/compat/src/memo.js
isomorfeus-preact-10.6.46 node_modules/preact/compat/src/memo.js
isomorfeus-preact-10.6.45 node_modules/preact/compat/src/memo.js
isomorfeus-preact-10.6.44 node_modules/preact/compat/src/memo.js
isomorfeus-preact-10.6.43 node_modules/preact/compat/src/memo.js
isomorfeus-preact-10.6.42 node_modules/preact/compat/src/memo.js
isomorfeus-preact-10.6.41 node_modules/preact/compat/src/memo.js
isomorfeus-preact-10.6.40 node_modules/preact/compat/src/memo.js
isomorfeus-preact-10.6.39 node_modules/preact/compat/src/memo.js
isomorfeus-preact-10.6.38 node_modules/preact/compat/src/memo.js
isomorfeus-preact-10.6.37 node_modules/preact/compat/src/memo.js
isomorfeus-preact-10.6.36 node_modules/preact/compat/src/memo.js
isomorfeus-preact-10.6.35 node_modules/preact/compat/src/memo.js
isomorfeus-preact-10.6.34 node_modules/preact/compat/src/memo.js
isomorfeus-preact-10.6.33 node_modules/preact/compat/src/memo.js
isomorfeus-preact-10.6.32 node_modules/preact/compat/src/memo.js
isomorfeus-preact-10.6.31 node_modules/preact/compat/src/memo.js