import React from 'react'; export const useLazyValue = ( value: string, ref: React.RefObject, options: IntersectionObserverInit = {rootMargin: '20px'} ): [string | null] => { const [lazyValue, setLazyValue] = React.useState(null); React.useEffect(() => { const observer = new IntersectionObserver((entries, observer): void => { entries.forEach(entry => { if (entry.isIntersecting) { setLazyValue(value); observer.unobserve(entry.target); } }); }, options); if (ref.current) { observer.observe(ref.current); } return (): void => observer.disconnect(); }, [options, ref, value]); return [lazyValue]; };