I'm trying to create a simple weather app. It gets an API response where weather description is represented by some string, for eg: "clear sky"
. I've created a JS object which maps those strings to the corresponding icon file paths which are in src/assets/images
folder:
const weatherIcons = {
// ...
"Heavy_Rain": "../assets/images/icons8-heavy-rain-96.png",
// ...
}
I'm trying to dynamically display that icon in my component:
<img className="img-fluid" src={require(weatherIcons.Heavy_Rain)} />
And I get the following error:
Error: Cannot find module '../assets/images/icons8-heavy-rain-96.png'
But it works perfectly if I use the string directly as a require
argument:
<img className="img-fluid" src={require("../assets/images/icons8-heavy-rain-96.png")} />
I use Parcel instead of Webpack to bundle project files. And I didn't run Create React App, so I don't have Public folder in my project.
What do I do to have my icons displayed eventually?