Skip to content

Instantly share code, notes, and snippets.

View mariorocha2308's full-sized avatar
🏠
Working from home

Mario Rocha mariorocha2308

🏠
Working from home
View GitHub Profile
@akramsaouri
akramsaouri / react-lazy-img-io.ts
Last active July 24, 2024 04:19
⚛️ Lazy load images in React with IntersectionObserver.
import React, { useEffect, useRef, FunctionComponent, HTMLProps } from 'react';
import 'intersection-observer'; // if you want to include a polyfill
/**
* Returns an IntersectionObserver that loads the image
* when it is at least {threshold}*100 visible in the viewport.
*
* PS: Cached on the window for performance
*/
function getImageLoaderObserver(