@unlazy/react
The React integration provides an UnLazyImage component for your React 19+ application.
Installation
Install the @unlazy/react package using your favorite package manager:
pnpm add -D @unlazy/reactyarn add -D @unlazy/reactnpm install -D @unlazy/reactImport the UnLazyImage component in your component file:
import { UnLazyImage } from '@unlazy/react'
export default function MyComponent() {
return (
<UnLazyImage
blurhash="LKO2:N%2Tw=w]~RBVZRi};RPxuwH"
srcSet="image-320w.jpg 320w, image-640w.jpg 640w"
autoSizes
/>
)
}UnLazyImage Component
The UnLazyImage component allows you to easily implement unlazy in your React application, providing a smoother image loading experience.
The component supports automatic calculation of the sizes attribute with the autoSizes prop. It also enables you to specify a blurhash for the blurry placeholder image.
Props
The UnLazyImage component accepts the following props:
| Prop | Type | Default | Description |
|---|---|---|---|
src | String | - | Image source URL to be lazy-loaded. |
srcSet | String | - | Image source set to be lazy-loaded. |
autoSizes | Boolean | false | Whether the sizes attribute should be automatically calculated. |
blurhash | String | - | A BlurHash string representing the blurry placeholder image. |
thumbhash | String | - | A ThumbHash string representing the blurry placeholder image. If both are provided, thumbhash takes precedence. |
placeholderSrc | String | - | Optional image source URL for a custom placeholder image. Ignored if a hash is provided. |
placeholderSize | Number | 32 | The size of the longer edge for BlurHash decoding. Ignored for ThumbHash. |
loading | String | 'lazy' | Loading strategy for the image ('lazy' or 'eager'). |
The component also accepts all standard <img> HTML attributes (e.g., alt, className, style).
Lifecycle
The component uses React's useEffect hook to initialize lazy loading when mounted:
- Renders with placeholder image or hash-generated placeholder
- Sets up lazy loading via
lazyLoad()from the core library - Decodes BlurHash/ThumbHash if provided (thumbhash takes precedence)
- Swaps
data-srctosrcwhen image enters viewport - Cleans up event listeners on unmount or when props change
Examples
return (
<UnLazyImage
width={800}
height={600}
blurhash="LKO2:N%2Tw=w]~RBVZRi};RPxuwH"
srcSet="image-320w.jpg 320w, image-640w.jpg 640w"
autoSizes
/>
)return (
<UnLazyImage
width={800}
height={600}
thumbhash="1QcSHQRnh493V4dIh4eXh1h4kJUI"
srcSet="image-320w.jpg 320w, image-640w.jpg 640w"
autoSizes
/>
)return (
<UnLazyImage
width={800}
height={600}
placeholderSrc="data:image/svg+xml, ..."
srcSet="image-320w.jpg 320w, image-640w.jpg 640w"
autoSizes
/>
)TIP
In each example, the sizes attribute is automatically calculated given the auto-sizes prop.
INFO
When using BlurHash, set explicit width and height props for optimal performance. Without these, BlurHash decoding falls back to rendered dimensions, which may cause performance delays on large images.