import { useEffect } from 'react';
declare global {
interface Window {
usePreloadImagesData?: Record<string, unknown[]>;
}
}
export const usePreloadImages = (imageSrcs: string[]): void => {
useEffect(() => {
const randomStr = Math.random().toString(32).slice(2) + Date.now();
window.usePreloadImagesData = window.usePreloadImagesData ?? {};
window.usePreloadImagesData[randomStr] = [];
for (const src of imageSrcs) {
// preload the image
const img = new Image();
img.src = src;
// keep a reference to the image
window.usePreloadImagesData[randomStr].push(img);
}
return () => {
delete window.usePreloadImagesData?.[randomStr];
};
}, [ imageSrcs ]);
};
/*
Supposing you have pictures: string[];
- array of pictures urls defined in your component's props.
You should define componentDidMount()
method in your component and then just create new Image object for each picture you want to be preloaded:
*/
componentDidMount() {
this.props.pictures.forEach((picture) => {
const img = new Image();
img.src = picture.fileName;
});
}