typescriptintermediate
Responsive Image Component
Image component with lazy loading, blur placeholder, error fallback, and srcset support.
typescriptPress ⌘/Ctrl + Shift + C to copy
import { useState, ImgHTMLAttributes } from 'react';
interface ResponsiveImageProps extends ImgHTMLAttributes<HTMLImageElement> {
fallback?: string;
blurDataUrl?: string;
}
export function ResponsiveImage({ fallback, blurDataUrl, className = '', style, ...props }: ResponsiveImageProps) {
const [loaded, setLoaded] = useState(false);
const [error, setError] = useState(false);
if (error && fallback) {
return <img {...props} src={fallback} className={className} alt={props.alt || ''} />;
}
return (
<div className={`relative overflow-hidden ${className}`} style={style}>
{blurDataUrl && !loaded && (
<img
src={blurDataUrl}
className="absolute inset-0 w-full h-full object-cover scale-110 blur-lg"
alt=""
aria-hidden
/>
)}
<img
{...props}
loading="lazy"
onLoad={() => setLoaded(true)}
onError={() => setError(true)}
className={`w-full h-full object-cover transition-opacity duration-300 ${loaded ? 'opacity-100' : 'opacity-0'}`}
alt={props.alt || ''}
/>
</div>
);
}Use Cases
- Image galleries
- Blog post images
Tags
Related Snippets
Similar patterns you can reuse in the same workflow.
typescriptintermediate
Next Image Responsive Patterns
Use next/image with responsive sizes, blur placeholders, and priority loading for optimal image delivery.
Best for: responsive images
#nextjs#image
typescriptbeginner
Next.js Image Optimization Patterns
Advanced next/image usage with responsive sizes, blur placeholders, and custom loaders.
Best for: Optimizing Core Web Vitals with proper image loading
#nextjs#image
typescriptbeginner
useDebounce — Debounced Value Hook
Debounce any rapidly-changing value with a configurable delay. Useful for search inputs and resize handlers.
Best for: Search-as-you-type
#hooks#debounce
typescriptbeginner
useMediaQuery — Responsive Breakpoint Hook
Subscribe to CSS media query changes and get a boolean flag for responsive rendering logic in React.
Best for: Responsive layouts
#hooks#responsive