DEV Community

Cover image for Image Lazy Loading
sundarbadagala
sundarbadagala

Posted on

142 2 2 2 2

Image Lazy Loading

INTRO ๐Ÿ”Š

Hello World! ๐ŸŒŽ
From now onwards, I want to start a new series named React optimization. We will discuss different react optimization techniques which help to enhance React Application performance.๐Ÿš€

Today's topic is IMAGE LAZY LOADING๐Ÿ”ฅ

We all know that assets (images) will take time to load once the application is rendered. Having more assets leads to more time to load the app. It affects the app's performance. To avoid this performance issue we can load images only when it is needed (need to display). Suppose we have one image at the top and another image bottom of the page. Initially, we need to render the image which is present at the top. Later we need to render the bottom image when we reach (scroll) that particular part only. It helps to reduce the app's initial rendering time and consume data when only needed.

ADVANTAGES ๐Ÿ”Š

๐Ÿ”ด Faster Initial Load Time: By deferring the loading of images that are not immediately visible, the initial page load time is reduced. This leads to a faster and more responsive user experience.

๐ŸŸ  Reduced Bandwidth Usage: Only the images that are actually needed are loaded, which reduces the overall bandwidth consumption. This is especially beneficial for users on mobile devices or with limited data plans.

๐ŸŸก Smoother Scrolling: Lazy loading images ensures that images load as the user scrolls, preventing janky or laggy scrolling experiences caused by heavy image loads.

๐ŸŸข Content Prioritization: Critical content and functionality can be loaded first, improving perceived performance and user engagement.

๐Ÿ”ต Improved Search Engine Ranking: Search engines favor faster-loading websites, which can positively impact your site's search engine rankings.

๐ŸŸฃ Reduced Bounce Rate: Faster loading times can lead to lower bounce rates, as users are more likely to stay on a site that loads quickly.
โšซ๏ธ Reduced Server Load: By loading only the necessary images, server load and resource usage are reduced, potentially lowering hosting costs.

โšช๏ธ Large Image Galleries: For applications with extensive image galleries or content-heavy pages, lazy loading helps ensure that performance remains optimal.

๐ŸŸค Progressive Enhancement: Users with slower internet connections or less capable devices still receive a functional and responsive experience as images load progressively.

APPROACH ๐Ÿ”Š

import React from "react";

function ImageLoading() {
  return (
    <div>
      <div style={{ height: "120vh" }}></div>
      <img src="https://picsum.photos/700" loading='lazy'/>
      <br />
      <img src="https://picsum.photos/800"  loading='lazy'/>
      <br />
      <img src="https://picsum.photos/900"  loading='lazy'/>
      <br />
      <img src="https://picsum.photos/1000"  loading='lazy'/>
      <br />
    </div>
  );
}

export default ImageLoading;
Enter fullscreen mode Exit fullscreen mode

If we observe the browser network by scrolling smoothly, we can observe assets calling according to scrolling. But not render all the images at a time.

๐Ÿ“Œ NOTE: The above method will not work for all browsers. So we need to install an external npm package to achieve this.

Install npm package react-lazy-load-image-component

Now we can implement our image lazy loading by using above package.

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

function ImageLoading() {
  return (
    <div>
      <div style={{ height: "120vh" }}></div>
      <LazyLoadImage src="https://picsum.photos/700" />
      <br />
      <LazyLoadImage src="https://picsum.photos/800" />
      <br />
      <LazyLoadImage src="https://picsum.photos/900" />
      <br />
      <LazyLoadImage src="https://picsum.photos/1000" />
      <br />
    </div>
  );
}

export default ImageLoading;
Enter fullscreen mode Exit fullscreen mode

react-lazy-load-image-component is one of the fine libraries that helps to achieve image lazy loading. This package has some other features but we are not discussing them now. You can check those on the npm site.

๐Ÿ“ŒNOTE: Even though image lazy loading is the best concept to enhance app performance, it has some limitations like better to avoid while implementing carousels and above the fold. It affects the user experience.

CONCLUSION ๐Ÿ”Š

I hope you guys like this concept. We will discuss the next concept in our next post.๐Ÿ‘๐Ÿป

Peace ๐Ÿ™‚

Sentry blog image

How I fixed 20 seconds of lag for every user in just 20 minutes.

Our AI agent was running 10-20 seconds slower than it should, impacting both our own developers and our early adopters. See how I used Sentry Profiling to fix it in record time.

Read more

Top comments (16)

Collapse
 
acidop profile image
Zeeshan โ€ข

Do you have any live demo that we can see?

Collapse
 
sundarbadagala081 profile image
sundarbadagala โ€ข

Hi, Zeeshan I'm trying to upload a video. But dev.to has no option to upload video files except images. ๐Ÿ˜Ÿ

Collapse
 
metz2000 profile image
James โ€ข

A recording is not live, live is something real time like watching a football game when it happens. A live demo is something that happens when one executes it (in real time) and can be inspected, modified (where is applicable i.e. source code is available).

For lazy loading the live demo should demonstrate on network tab that images below the fold are not loaded until scrolled into view.

Like this:
enviragallery.com/demo/lazy-loadin...

May I suggest to get more experience before publishing articles that are supposed to teach others. Also, add some value on top of documentation and other articles otherwise your article is just a copy (and you didn't even mentioned your sources which is not nice).

Collapse
 
alphacentaurrii profile image
เผบRakesh Singhเผป โ€ข

How about gif

Collapse
 
oscarrodar profile image
Oscar Rodriguez โ€ข

Add a GIF instead! Licecap is a simple lightweight GIF creator.

Collapse
 
lazylad profile image
Tathagat โ€ข

You can upload gif

Collapse
 
gihanrangana profile image
GihanRangana โ€ข

use stackblitz.com/ and embed it in here

Collapse
 
evart_valls_303ec85853989 profile image
Evart Valls โ€ข

Hi Sun... Thanks for the explanation. I am a trainee and I don't know if this question is intelligent.
Could you post the video on youtube and send us the link?

Collapse
 
tainv1101 profile image
tainv1101 โ€ข

I see the demo link in this npm

Collapse
 
lixingjuan profile image
lixingjuan โ€ข

Image description

ๅผบ่ฟซ็—‡ๆžๅบฆ่ˆ’้€‚๐Ÿ™‚โ€โ†”๏ธ

Collapse
 
sundarbadagala081 profile image
sundarbadagala โ€ข โ€ข Edited

@lixingjuan I usually add emojis๐Ÿ™‚ in my posts๐Ÿ“. These will make posts so colourful๐ŸŒˆ and fresh โ„๏ธ

Collapse
 
shaogat_alam_1e055e90254d profile image
Shaogat Alam โ€ข

Interesting topic! Everything is explained articulately and clearly. For your project, consider checking out this free npm package: select-paginated.

Collapse
 
hesxenon profile image
hesxenon โ€ข

how exactly does it differ from the native image lazy loading?

Collapse
 
tanveermughal profile image
Tanveer Mughal โ€ข

Great, it would help more if you could also explain how this package works under the hood.

Collapse
 
cjcheshire profile image
Chris Cheshire โ€ข

Those looking for a demo the library links to one in their readme:

albertjuhe.com/react-lazy-load-ima...

Collapse
 
tanmay_dobariya_31d67bd86 profile image
Tanmay Dobariya โ€ข

Helpful๐Ÿ‘

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

Sentry image

See why 4M developers consider Sentry, โ€œnot bad.โ€

Fixing code doesnโ€™t have to be the worst part of your day. Learn how Sentry can help.

Learn more

๐Ÿ‘‹ Kindness is contagious

Please leave a โค๏ธ or a friendly comment on this post if you found it helpful!

Okay