Skip to content

Instantly share code, notes, and snippets.

@mkhoussid
Created November 16, 2020 08:22
Show Gist options
  • Save mkhoussid/c960ba200d6ef9142b80769a7c84e5f1 to your computer and use it in GitHub Desktop.
Save mkhoussid/c960ba200d6ef9142b80769a7c84e5f1 to your computer and use it in GitHub Desktop.
usePreloadImages hook (download images in the background after DOM has rendered)
// This preloads images AFTER DOM has loaded
// See PreloadedImages.tsx for loading images before DOM is ready (https://gist.github.com/mkhoussid/d6a1a7b3d029c42bbcb45bdc929602c8)
import React from 'react';
export default function usePreloadImages(imageUrls: string[]): void {
React.useEffect(() => {
imageUrls.forEach((imageUrl) => {
const image = new Image();
image.src = imageUrl;
});
}, []);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment