React Lazy Load Component

4.0.1 · active · verified Tue Apr 21

react-lazy-load is a React component designed to defer the loading of content, making web applications more performant by only rendering elements when they become visible within the viewport. The current stable version is 4.0.1, which includes support for React 18 and TypeScript, and internally utilizes the browser's Intersection Observer API for efficient detection of visibility. This library has seen an active development cadence, with a significant v4 major release focused on modernizing its approach by removing external dependencies and leveraging native browser capabilities. Its key differentiators include simplicity, automatic detection of scrolling containers, and a focus on performance by avoiding manual scroll watching in favor of Intersection Observer.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates basic usage of the LazyLoad component, showing how to wrap an image, specify height, use `offset` to load content before it's fully visible, and `threshold` for a specific visibility percentage (v4+), including a callback for content visibility.

import React from 'react';
import LazyLoad from 'react-lazy-load';

const MyImageGallery = () => (
  <div>
    <h1>My Lazy Loaded Gallery</h1>
    <p>Scroll down to see images load as they enter the viewport.</p>
    <div style={{ height: '800px', background: '#eee' }}>
      {/* Placeholder to create scroll space */}
      Content above the fold.
    </div>
    <LazyLoad height={300} offset={200} onContentVisible={() => console.log('Image 1 loaded!')}>
      <img 
        src='https://via.placeholder.com/600x300/FF5733/FFFFFF?text=Lazy+Image+1'
        alt='Placeholder Image 1'
        style={{ display: 'block', maxWidth: '100%' }}
      />
    </LazyLoad>
    <div style={{ height: '500px', background: '#ddd' }}>
      {/* More placeholder content */}
      More content to scroll past.
    </div>
    <LazyLoad height={400} threshold={0.75} onContentVisible={() => console.log('Image 2 loaded!')}>
      <img 
        src='https://via.placeholder.com/800x400/33FF57/000000?text=Lazy+Image+2'
        alt='Placeholder Image 2'
        style={{ display: 'block', maxWidth: '100%' }}
      />
    </LazyLoad>
    <div style={{ height: '1000px', background: '#ccc' }}>
      {/* Final placeholder content */}
      Even more content at the bottom.
    </div>
  </div>
);

export default MyImageGallery;

view raw JSON →