I have a React component where I have an image path imported and then it's name passed in the img src, like this:
import { Myimage } from './images/image.jpg';
const MyComponent = (props) => (
<div>
<img src={Myimage} alt="" />
</div>
);
The above works fine but I want to pass the name of the import using a prop but it's returning a string so it won't work.
Here's the code:
const MyComponent = (props) => (
<div>
<img src={props.myprop} alt="" />
</div>
);
Here is the call:
<MyComponent myprop="Myimage" />
How can I pass an import name in the img src?
Try this...
<MyComponent myprop={Myimage} />
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With