@shopify/polaris
Version:
Shopify’s product component library
19 lines (18 loc) • 677 B
JavaScript
import { useRef } from 'react';
const UNIQUE_IDENTIFIER = Symbol('unique_identifier');
/**
* useLazyRef provides a lazy initial value, similar to lazy
* initial state the initialValue is the value used during
* initialization and disregarded after that.
* @param initialValue - A function that will return the initial
* value and be disregarded after that
* @returns MutableRefObject<T> - Returns a ref object with the
* results from invoking initial value
*/
export function useLazyRef(initialValue) {
const lazyRef = useRef(UNIQUE_IDENTIFIER);
if (lazyRef.current === UNIQUE_IDENTIFIER) {
lazyRef.current = initialValue();
}
return lazyRef;
}