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.9.0 node_modules/preact/compat/src/memo.js
isomorfeus-preact-10.8.2 node_modules/preact/compat/src/memo.js
isomorfeus-preact-10.8.1 node_modules/preact/compat/src/memo.js
isomorfeus-preact-10.8.0 node_modules/preact/compat/src/memo.js
isomorfeus-preact-10.7.3 node_modules/preact/compat/src/memo.js
isomorfeus-preact-10.7.2 node_modules/preact/compat/src/memo.js
isomorfeus-preact-10.7.1 node_modules/preact/compat/src/memo.js
isomorfeus-preact-10.7.0 node_modules/preact/compat/src/memo.js
isomorfeus-preact-10.6.62 node_modules/preact/compat/src/memo.js
isomorfeus-preact-10.6.61 node_modules/preact/compat/src/memo.js
isomorfeus-preact-10.6.60 node_modules/preact/compat/src/memo.js
isomorfeus-preact-10.6.59 node_modules/preact/compat/src/memo.js
isomorfeus-preact-10.6.58 node_modules/preact/compat/src/memo.js
isomorfeus-preact-10.6.57 node_modules/preact/compat/src/memo.js
isomorfeus-preact-10.6.56 node_modules/preact/compat/src/memo.js
isomorfeus-preact-10.6.55 node_modules/preact/compat/src/memo.js
isomorfeus-preact-10.6.54 node_modules/preact/compat/src/memo.js
isomorfeus-preact-10.6.53 node_modules/preact/compat/src/memo.js
isomorfeus-preact-10.6.52 node_modules/preact/compat/src/memo.js
isomorfeus-preact-10.6.51 node_modules/preact/compat/src/memo.js