Lazy Loading Images With Intersection Observer in React

Creating a Gatsby Image style custom React component

Lazy loading is a common performance optimization technique followed by almost all asset-heavy websites. We often come across web pages where a blurred version of the image loads up and is then followed up with a high-resolution image. Although the total time taken to load up the content is long, it has a perceivable effect on user experience.

This entire interaction is a three-step process:

  • Wait for the content to come into the view before even starting to load the image.
  • Once the image is in view, a lightweight thumbnail is loaded with a blur effect and the resource fetch request for the original image is made.
  • Once the original image is fully loaded, the thumbnail is hidden and the original image is shown.

If you have ever used Gatsby, then you would have come across a** Gatsby Image** component that does the same for you. In this article, we will implement a similar component that can be used as a generic component. Although Gatsby Image does a lot more than blur and load images, we will focus on the simple thing:

Let’s Build It.

The first step to building the entire thing is to create a layout of your image components.

This part is pretty straightforward. For the purpose of the article, we will dynamically iterate over a set of images and render an ImageRenderer component.

#react #javascript #programming

Lazy Loading Images with intersection Observer In React
1.40 GEEK