React Image and Lazy-Loading Libraries
|
This content was generated with the assistance of AI and should be verified against the official documentation before being relied on in production. |
Baseline date: 2026-10-07. Versions are the latest on npm on that date. Since browsers lazy-load natively, most of these libraries are now optional. This page says what each does, how active it is and what to use instead. The React fundamentals are in the React section, and the native attributes are in native HTML images.
The native baseline
export function Cover({ src, srcSet, alt, width, height }: CoverProps) {
return (
<img src={src} srcSet={srcSet} sizes="(min-width: 60em) 20vw, 50vw"
alt={alt} width={width} height={height} loading="lazy" decoding="async" />
);
}
React passes loading, decoding and fetchPriority straight to the DOM. For the LCP image, drop loading="lazy".
That covers lazy loading, responsive sources and layout shift without a dependency. What it lacks is a fallback chain,
a custom trigger distance and a built-in effect, which is what the libraries add.
react-lazyload
react-lazyload (3.2.1) wraps content in a LazyLoad component that renders it
when it scrolls near the viewport.
import LazyLoad from 'react-lazyload';
<LazyLoad height={300} offset={200} once placeholder={<div className="skeleton" />}>
<img src={url} alt={alt} width={200} height={300} />
</LazyLoad>
It works with any content, not only images, and it uses scroll listeners rather than an IntersectionObserver by default. Activity is low. Prefer native lazy loading for images, or IntersectionObserver for other content.
react-image
react-image (4.1.0) offers an Img component and a useImage hook with a list of
sources to try in order, and loader and fallback elements. It has not had a release since 2023.
import { useImage } from 'react-image';
function Cover({ srcList }: { srcList: string[] }) {
const { src, isLoading, error } = useImage({ srcList, useSuspense: false });
if (isLoading) return <div className="skeleton" />;
if (error) return <div className="no-cover">No cover</div>;
return <img src={src} alt="" />;
}
The fallback chain is useful for user avatars (try the user’s image, then a generated one). It does not do responsive
images: it picks one URL, so you lose srcset. Verify that the version still works with your React major version.
react-lazy-load-image-component
react-lazy-load-image-component (1.6.3) provides
LazyLoadImage with a built-in placeholder, an effect (blur, black and white, opacity), and a trackWindowScroll HOC for
many images in a scrolling container.
import { LazyLoadImage } from 'react-lazy-load-image-component';
import 'react-lazy-load-image-component/src/effects/blur.css';
<LazyLoadImage src={url} alt={alt} width={200} height={300} effect="blur" placeholderSrc={tinyUrl} />
The blur effect shows the image blurred while the file loads, and a placeholderSrc can be a small image or a data URL
decoded from a BlurHash, see BlurHash in the browser.
A small IntersectionObserver hook
When you need to control the trigger (a carousel, an infinite list, a video), a hook is a dozen lines and has no dependency:
import { useEffect, useRef, useState } from 'react';
export function useInView<T extends Element>(rootMargin = '200px') {
const ref = useRef<T>(null);
const [inView, setInView] = useState(false);
useEffect(() => {
const node = ref.current;
if (!node) return;
const observer = new IntersectionObserver(([entry]) => {
if (entry.isIntersecting) { setInView(true); observer.disconnect(); }
}, { rootMargin });
observer.observe(node);
return () => observer.disconnect();
}, [rootMargin]);
return [ref, inView] as const;
}
Reference: MDN Intersection Observer API.
Choosing a strategy
Most decisions collapse to two questions: does the browser already know how to do what you need, and is the content an image?
Worked example: a lazy cover with a BlurHash
The following component puts the pieces together for the cover grid: the box reserves its space from the aspect ratio, a
BlurHash canvas fills it at once, the <img> is only rendered when the box is within 300 pixels of the viewport, and the
canvas fades out when the file has loaded. It reuses the useInView hook from above and the blurhashToCanvas helper from
BlurHash in the browser.
import { useEffect, useRef, useState } from 'react';
import { blurhashToCanvas } from './blurhash';
import { useInView } from './use-in-view';
type CoverProps = { src: string; srcSet: string; alt: string; width: number; height: number; blurhash: string };
export function LazyCover({ src, srcSet, alt, width, height, blurhash }: CoverProps) {
const [boxRef, inView] = useInView<HTMLDivElement>('300px');
const canvasRef = useRef<HTMLCanvasElement>(null);
const [loaded, setLoaded] = useState(false);
useEffect(() => {
if (canvasRef.current) blurhashToCanvas(blurhash, canvasRef.current);
}, [blurhash]);
return (
<div ref={boxRef} style={{ position: 'relative', aspectRatio: `${width} / ${height}` }}>
<canvas ref={canvasRef} aria-hidden="true"
style={{ position: 'absolute', inset: 0, width: '100%', height: '100%', opacity: loaded ? 0 : 1, transition: 'opacity .3s' }} />
{inView && (
<img src={src} srcSet={srcSet} sizes="(min-width: 60em) 20vw, 50vw" alt={alt} width={width} height={height}
decoding="async" onLoad={() => setLoaded(true)}
ref={(img) => { if (img?.complete) setLoaded(true); }}
style={{ position: 'absolute', inset: 0, width: '100%', height: '100%' }} />
)}
</div>
);
}
Three details matter. The observer sits on the wrapper div, not on the image, because the <img> does not exist before
it is needed. The ref callback on the image covers a cached image whose load event fired before React attached the handler.
And the component does not use loading="lazy" itself: the observer is the lazy mechanism, so adding both would delay the
download twice. If you do not need the custom trigger, replace the observer with loading="lazy" and render the <img>
immediately.
On the first render the <img> is absent and the observer is created with the 300px margin. After one intersecting
entry the image appears and the observer is disconnected, so there is no leak when many covers share a page. In a real list, wrap the component in React.memo and give
the list stable keys so scrolling does not recreate observers.
Testing the lazy behaviour
jsdom has no IntersectionObserver, so tests install a small stub and trigger the callback by hand. jsdom has no canvas either, so the BlurHash helper is mocked. This is enough to assert the contract that matters: nothing is rendered until the box is near the viewport, and it is rendered once.
import { render, screen, act } from '@testing-library/react';
import { expect, test, vi } from 'vitest';
import { LazyCover } from './LazyCover';
vi.mock('./blurhash', () => ({ blurhashToCanvas: () => true }));
test('renders the image only after the box intersects', () => {
let callback: IntersectionObserverCallback = () => {};
vi.stubGlobal('IntersectionObserver', class {
constructor(cb: IntersectionObserverCallback) { callback = cb; }
observe() {}
disconnect() {}
});
render(<LazyCover src="/a.jpg" srcSet="/a.jpg 640w" alt="Cover" width={200} height={300} blurhash="T3QYhT}WfQ=vjtfQfQfQfQ=vjtfQ" />);
expect(screen.queryByAltText('Cover')).toBeNull();
act(() => callback([{ isIntersecting: true } as IntersectionObserverEntry], {} as IntersectionObserver));
expect(screen.getByAltText('Cover')).toBeTruthy();
});
Which to use
| Option | Status | Needed? | When to choose it |
|---|---|---|---|
Native |
Browser feature |
Default |
Almost always. No dependency, responsive images, works with SSR. |
react-lazyload 3.2.1 |
Low activity |
Rarely |
Lazy-rendering non-image components with a scroll container, if you are already using it. |
react-image 4.1.0 |
No release since 2023 |
Sometimes |
Fallback chains for user-provided images. Check compatibility with your React version. |
react-lazy-load-image-component 1.6.3 |
Check the repository for recent activity |
Sometimes |
A ready-made blur or opacity effect over the placeholder, with minimal code. |
IntersectionObserver hook |
Browser feature |
When native is not enough |
Custom triggers and non-image content. |
|
Maintained with the framework |
In Next.js apps |
See Next.js Image. |
Whatever you choose, give every image width and height (or an aspect ratio) and use an optimised format. The libraries
only decide when to load.