Ask any question about Web Development here... and get an instant response.
How do you implement lazy loading for images in a React app?
Asked on Dec 20, 2025
Answer
Lazy loading images in a React app can significantly improve performance by deferring the loading of images until they are needed. This can be achieved using the `loading="lazy"` attribute in HTML5 or by utilizing libraries like `react-lazyload` or `IntersectionObserver` API for more control.
<!-- BEGIN COPY / PASTE -->
import React from 'react';
const LazyImage = ({ src, alt }) => (
<img src={src} alt={alt} loading="lazy" />
);
export default LazyImage;
<!-- END COPY / PASTE -->Additional Comment:
- Using the `loading="lazy"` attribute is the simplest method and is natively supported in modern browsers.
- For more advanced scenarios, consider using the `IntersectionObserver` API to implement custom lazy loading logic.
- Libraries like `react-lazyload` offer additional features such as placeholders and fade-in effects.
- Ensure that your images have width and height attributes to prevent layout shifts.
Recommended Links:
