How to preload images in React.js?

前端 未结 7 637
夕颜
夕颜 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:20

    The important thing to do is to save the image variable as PERSISTANT.

    In a persistant context,in a global var or in a component that will not be unmounted before display the images :

    window.preloadedPictures = []
    

    In my component :

    var preloadedData = pictures.map((picture) => {
        const img = new Image();
        img.src = picture.fileName;
        return img
    });
    
    myContextOrGlobalVar.preloadedPictures = preloadedData ///IMPORTANT 
    

提交回复
热议问题