How to preload images in React.js?

前端 未结 7 638
夕颜
夕颜 2020-12-10 11:14

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

7条回答
  •  青春惊慌失措
    2020-12-10 11:24

    If you have some images still reloading after being cached, try to store them in the window object :

    componentDidMount() {
        const imagesPreload = [image1, image2, image3];
        imagesPreload.forEach((image) => {
            const newImage = new Image();
            newImage.src = image;
            window[image] = newImage;
        });
    }
    

    (you don't need to call images from the window object)

提交回复
热议问题