File "use-shallow-equal.ts"

Full Path: /home/vantageo/public_html/cache/cache/cache/cache/cache/cache/cache/.wp-cli/wp-content/plugins/woocommerce/packages/woocommerce-blocks/assets/js/base/hooks/use-shallow-equal.ts
File size: 824 bytes
MIME-type: text/x-java
Charset: utf-8

/**
 * External dependencies
 */
import { useRef } from '@wordpress/element';
import isShallowEqual from '@wordpress/is-shallow-equal';

/**
 * A custom hook that compares the provided value across renders and returns the
 * previous instance if shallow equality with previous instance exists.
 *
 * This is particularly useful when non-primitive types are used as
 * dependencies for react hooks.
 *
 * @param {*} value Value to keep the same if satisfies shallow equality.
 *
 * @return {*} The previous cached instance of the value if the current has
 *                 shallow equality with it.
 */
export function useShallowEqual< T >( value: T ): T | undefined {
	const ref = useRef< T >();
	if ( ref.current === undefined || ! isShallowEqual( value, ref.current ) ) {
		ref.current = value;
	}
	return ref.current;
}