I had the same scenario where I have to pick images
or SVG
s from the folder and display it. Below is the process I followed :
Folder structure
-public
-src
|-component
|-images
|-1.png
|-2.png
|-3.png
.
.
|-some x number.png
In component
where ever you want to consume the image
there, you have to do this:
export default App;
import React from 'react';
import ReactDOM from 'react-dom';
var listOfImages =[];
class App extends React.Component{
importAll(r) {
return r.keys().map(r);
}
componentWillMount() {
listOfImages = this.importAll(require.context('./images/', false, /.(png|jpe?g|svg)$/));
}
render(){
return(
<div>
{
listOfImages.map(
(image, index) => <img key={index} src={image} alt="info"></img>
)
}
</div>
)
}
}
It worked for me; Give it a try.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…