- Since you are using webpack, have a look at require.context . You should be able to import all png files in
'/img' to images variable. Then you can use image by images["img(n).png"].
function importAll(r) {
let images = {};
r.keys().map((item, index) => { images[item.replace('./', '')] = r(item); });
return images;
}
const images = importAll(require.context('/img', false, '/\.jpg/'));
<img src={images["img1.png"]} />
- In another way, you can use a file dedicated to these imports :
images.js :
import img1 from "/img/img1.jpg"
import img2 from "/img/img2.jpg"
import img3 from "/img/img3.jpg"
.
.
.
import img(n) from "/img/img(n).jpg"
export default [
img1,
img2,
...
];
Then import this array in one line in other files :
import imgs from './images';
P.S. please refer my accepted answer on similar question.