Last active
May 5, 2024 12:04
-
-
Save letelete/daf731f46b0804e1bf8062ea9be3ec54 to your computer and use it in GitHub Desktop.
Preload all images. Insert them to the DOM, and hide from the user. Avoid unnecessary re-renders with `memo`.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Image from 'next/image'; | |
import { memo } from 'react'; | |
import { ImageItem } from '~lib/images/provider'; | |
export interface ImagesPreloaderProps { | |
items: ImageItem[]; | |
width?: number; | |
height?: number; | |
} | |
const ImagesPreloader = memo( | |
({ items, width = 1, height = 1 }: ImagesPreloaderProps) => ( | |
<div className='hidden' aria-hidden> | |
{items.map((item) => ( | |
<Image | |
aria-hidden | |
key={`preloaded-${JSON.stringify(item.src)}`} | |
className='hidden' | |
src={item.src} | |
alt='' | |
width={width} | |
height={height} | |
priority | |
/> | |
))} | |
</div> | |
) | |
); | |
ImagesPreloader.displayName = 'ImagesPreloader'; | |
export { ImagesPreloader }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment