DEV Community

Cover image for HOW TO LAZY LOAD IMAGE IN REACTJS
Candie
Candie

Posted on

9

HOW TO LAZY LOAD IMAGE IN REACTJS

Lazy loading images in your react project optimizes your website and also makes it visually appealing to visitors.

We can achieve this in various ways, but I will only highlight two ways

APPROACH 1

The HTML image tag now accepts a loading attribute that takes in either the lazy attribute or the trigger attribute

The lazy attribute tells the browser to only load and download images currently on the browser's viewport and defer other images load and download to when the user scrolls to them.

The eager attribute tells the browser to load all images immediately.

For optimization purposes, adding the lazy attribute reduces build and browser load time.

<img loading='lazy' />
Enter fullscreen mode Exit fullscreen mode

APPROACH 2
We will be using a third party react library to optimize image load and download.

The library is react-lazy-load-image-component

Run the command below to install this package to your react project.

npm install react-lazy-load-image-component
Enter fullscreen mode Exit fullscreen mode

If you're building a large project where you will be using the img tag a lot, I will advise you create a reusable component.

I will name mine Image.jsx

Step 1: Import the react-lazy-load-image-component package to the file
Step 2: Import the lazy load effect you would like to use, iIwill be using blur here.
Step 3: Render the LazyLoadImage component in the file and pass in the needed props as illustrated below
Step 4: Save the file and preview the image

You have successfully optimized your project by implementing lazy image loading.

import { LazyLoadImage } from "react-lazy-load-image-component";
import "react-lazy-load-image-component/src/effects/blur.css";

export default function Image({src, styles}) {
  return (
    <>
      <LazyLoadImage
        alt={alt}
        className="gallery-img"
        effect="blur"
        src={src}
        width="100%"
        style={styles}
      />
    </>
  );
}
Enter fullscreen mode Exit fullscreen mode

You can customize the effect to your taste, read more about the package here;

Let me know if this was helpful to you in the comment section

SurveyJS custom survey software

Build Your Own Forms without Manual Coding

SurveyJS UI libraries let you build a JSON-based form management system that integrates with any backend, giving you full control over your data with no user limits. Includes support for custom question types, skip logic, an integrated CSS editor, PDF export, real-time analytics, and more.

Learn more

Top comments (3)

Collapse
 
eraj_eraj_c3a82474bb91404 profile image
Eraj Eraj

Very helpful,especially for ones who are new to this library.Thanks a million.

Some comments may only be visible to logged-in visitors. Sign in to view all comments.

Cloudinary image

Optimize, customize, deliver, manage and analyze your images.

Remove background in all your web images at the same time, use outpainting to expand images with matching content, remove objects via open-set object detection and fill, recolor, crop, resize... Discover these and hundreds more ways to manage your web images and videos on a scale.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay