Frontend School

Lazy Loading Image Gallery

Machine Coding
easy

Lazy Loading Image Gallery

Easy
React
TypeScript
Frontend
Performance
IntersectionObserver

Overview

Modern web applications frequently display extensive image galleries, product listings, or user-generated content. Loading all images simultaneously can significantly degrade page load performance and user experience, especially on slower networks or devices. Implementing a lazy loading strategy ensures that images are only fetched and rendered when they are about to enter the user's viewport, conserving bandwidth and improving perceived performance.

What you need to do / Task

Your task is to implement a lazy loading mechanism for a grid of images. You will be provided with a basic React application structure and mock image data. Your solution should:

  1. Render Image Cards: Display a list of ImageCard components, each representing an image from the provided mock data.
  2. Implement Lazy Loading: Utilize the IntersectionObserver API to detect when an ImageCard (specifically its <img> element) enters or is near the viewport.
  3. Conditional src Assignment: Initially, <img> elements should not have their actual src attribute set. Only assign the image's url to the src attribute once the ImageCard becomes visible (i.e., intersects with the viewport).
  4. Loading and Error States: Provide visual feedback for images that are either still loading or have failed to load.

UI/UX expectations

  • The application should display a scrollable grid of image cards.
  • Each ImageCard should maintain a fixed aspect ratio or size (e.g., 280px x 280px).
  • Before an image loads, a placeholder (e.g., a simple spinner or gray box) should be visible within the card.
  • Upon successful loading, the actual image should seamlessly replace the placeholder.
  • If an image fails to load, an error indicator (e.g., "Failed to load") should be displayed.
  • Scrolling should be smooth, without noticeable jank or flickering as images load.

Acceptance criteria

  • Initial Load: Only images within the initial viewport are loaded on page render. Images outside the viewport show a placeholder.
  • Scroll-triggered Load: As the user scrolls down, images entering the viewport dynamically load their content.
  • No Redundant Loads: Images that have already successfully loaded should not re-trigger network requests if they scroll out of view and then back in.
  • IntersectionObserver Usage: The solution correctly leverages the IntersectionObserver API for visibility detection.
  • Visual Feedback: Clear visual indicators are present for loading and error states of individual images.
  • Clean Cleanup: The IntersectionObserver is properly disconnected when the component unmounts or once an image has loaded.

Constraints

Constraints & assumptions

  • You can assume a modern browser environment that fully supports the IntersectionObserver API.
  • Image URLs are provided as mock data; no API calls are required to fetch image metadata.
  • Focus on client-side rendering with React and TypeScript.
  • The provided styles.css can be used to achieve the desired layout and visual states.

Out of scope

  • Implementing a custom scroll event listener-based lazy loader (you must use IntersectionObserver).
  • Advanced performance optimizations like srcset, sizes, or different image formats.
  • Server-side rendering (SSR) or static site generation (SSG).
  • Complex error retry mechanisms.

What interviewers evaluate

  • Correctness and Idiomatic React: Proper use of React hooks (useState, useEffect, useRef) and IntersectionObserver.
  • State Management: Effective management of image loading, loaded, and error states within the component.
  • Performance Awareness: Demonstrating an understanding of how to prevent unnecessary network requests and render efficiently.
  • Code Quality: Clean, readable, and maintainable code with appropriate type definitions.

Loading editor...