import {useRef} from 'react'; type UninitializedMarker = Readonly> | symbol; const UNINITIALIZED: UninitializedMarker = typeof Symbol === 'function' && typeof Symbol() === 'symbol' ? Symbol() : Object.freeze({}); export default function useStable(initialValueCallback: () => T): T { const ref = useRef(UNINITIALIZED); if (ref.current === UNINITIALIZED) { ref.current = initialValueCallback(); } return ref.current as T; }