Ask any question about Web Development here... and get an instant response.
Post this Question & Answer:
How can I improve lazy loading performance for images in a React app?
Asked on Feb 05, 2026
Answer
Improving lazy loading performance for images in a React app involves optimizing how images are loaded and displayed as users scroll through the page. This can be achieved by using the Intersection Observer API to efficiently detect when images enter the viewport and load them only at that moment.
<!-- BEGIN COPY / PASTE -->
import React, { useEffect, useRef, useState } from 'react';
const LazyImage = ({ src, alt }) => {
const [isVisible, setIsVisible] = useState(false);
const imgRef = useRef();
useEffect(() => {
const observer = new IntersectionObserver(
([entry]) => {
if (entry.isIntersecting) {
setIsVisible(true);
observer.disconnect();
}
},
{ threshold: 0.1 }
);
if (imgRef.current) {
observer.observe(imgRef.current);
}
return () => {
if (imgRef.current) {
observer.unobserve(imgRef.current);
}
};
}, []);
return (
<img
ref={imgRef}
src={isVisible ? src : ''}
alt={alt}
loading="lazy"
style={{ minHeight: '200px', minWidth: '300px' }}
/>
);
};
export default LazyImage;
<!-- END COPY / PASTE -->Additional Comment:
- Use the "loading" attribute with "lazy" to defer loading off-screen images.
- Ensure images have a minimum height and width to prevent layout shifts.
- Consider using a placeholder or low-quality image preview (LQIP) for better UX.
- Test performance improvements using browser developer tools to measure load times.
Recommended Links:
