Importing multiple files in react

前端 未结 5 900
滥情空心
滥情空心 2020-12-24 07:01

I am using create-react-app for my react project. It has got webpack configured for importing images. I wish to import multiple images (say 10) from a images folder into a c

5条回答
  •  一个人的身影
    2020-12-24 07:24

    You can't use a single import statement, but you can do this:

    function importAll(r) {
        let images = {};
        r.keys().map((item, index) => { images[item.replace('./', '')] = r(item); });
        return images;
    }
    
    const images = importAll(require.context('./images', false, '/\.png/'));
    
    
    

    Source.

提交回复
热议问题