I have a component, in which I've passed image url, but getting compiler error.
Service.js
import React from 'react';
const Service = (props) => {
return (
<div className="col-sm-6 col-md-6 col-lg-6 col-6">
<div className="service">
<img src={require(props.URL)} alt="pencil_logo" />
<h4>{props.Title}</h4>
<p>{props.Description}</p>
</div>
</div>
);
};
export default Service;
About.js
const About = (props) => {
console.log(props);
let services = null;
services = props.Services.map((x, index) => {
return <Service {...x} key={index} />;
});
return(
<div>{services}</div>
);
};
export default About;
props.Services has the following values
{
"Services": [
{
"Description": "testdata",
"Title": "testdata",
"URL": "../../../assets/images/site/pencil_tip.svg"
},
{
"Description": "testdata2",
"Title": "testdata2",
"URL": "../../../assets/images/site/pencil_tip2.svg"
},
{
"Description": "testdata3",
"Title": "testdata3",
"URL": "../../../assets/images/site/pencil_tip3.svg"
}
]
}
When I run this app, i'm getting this erro
Uncaught Error: Cannot find module '../../../assets/images/site/pencil_tip.svg'
But When I use like this, it works perfectly fine.
import pencil from'../../../assets/images/site/pencil_tip.svg';
...
<img src={pencil} alt="pencil_logo" />
Why it's not finding the image dynamically and how to fix this and why it finds the image if hardcoded?
UPDATE #1:
I'm getting this warning in my terminal while building
Compiled with warning
../xx/xx/Service.js
Critical dependency: the request of a dependency is an expression