How to preload images in React.js? I have dropdown select component which works like menu , but i have to preload image icons for items,because sometimes they are not visib
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;
});
}
It forces browser to load all images. This example is written in TypeScript.