Question: React Import Icons / Images in bulk

Question

React Import Icons / Images in bulk

Answers 1
Added at 2016-12-31 15:12
Tags
Question

I have 100's of Icons and Images to be imported. Is there any way to eliminate writing so many import statements at the top of the page? I was thinking to write a import statements in a separate file and embed that at the top.

import basicAmenitiesIcon from '../../../../images/icons/wifi-sign.png';
import parkingIcon from '../../../../images/icons/parking.png';
...

Any other way of solving it? I'm using webpack and here is the config:

{
    test: /\.(jpe?g|png|gif|svg)$/i,
    loaders: [
        'file?hash=sha512&digest=hex&name=[hash].[ext]',
        'image-webpack?bypassOnDebug&optimizationLevel=7&interlaced=false'
    ]
}
Answers to

React Import Icons / Images in bulk

nr: #1 dodano: 2016-12-31 20:12

Yes, it is posible, see my answer here: http://stackoverflow.com/a/41410938/646156

var context = require.context('../../../../images/icons', true, /\.(png)$/);
var files={};

context.keys().forEach((filename)=>{
  files[filename] = context(filename);
});
console.log(files); //you have file contents in the 'files' object, with filenames as keys
Source Show
◀ Wstecz