Ask any question about Web Development here... and get an instant response.
Post this Question & Answer:
How do you implement lazy loading for images in a React app?
Asked on May 11, 2026
Answer
Lazy loading images in a React app improves performance by only loading images when they enter the viewport. This can be achieved using the `loading` attribute in HTML or by leveraging libraries like `react-lazyload` for more control.
<!-- BEGIN COPY / PASTE -->
import React from 'react';
import LazyLoad from 'react-lazyload';
const LazyImage = ({ src, alt }) => (
<LazyLoad height={200} offset={100}>
<img src={src} alt={alt} loading="lazy" />
</LazyLoad>
);
export default LazyImage;
<!-- END COPY / PASTE -->Additional Comment:
- Use the `loading="lazy"` attribute for native browser support, which is simple and effective.
- For more advanced scenarios, `react-lazyload` provides options like placeholders and offsets.
- Ensure that images have defined dimensions to prevent layout shifts.
- Test lazy loading in various browsers to ensure compatibility and performance.
Recommended Links:
