Lazy Loading Image Gallery
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.
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:
ImageCard components, each representing an image from the provided mock data.IntersectionObserver API to detect when an ImageCard (specifically its <img> element) enters or is near the viewport.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).ImageCard should maintain a fixed aspect ratio or size (e.g., 280px x 280px).IntersectionObserver Usage: The solution correctly leverages the IntersectionObserver API for visibility detection.IntersectionObserver is properly disconnected when the component unmounts or once an image has loaded.Constraints
IntersectionObserver API.styles.css can be used to achieve the desired layout and visual states.IntersectionObserver).srcset, sizes, or different image formats.useState, useEffect, useRef) and IntersectionObserver.Loading editor...