Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React passing import name as prop on img src passing as string

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?


1 Answers

Try this...

<MyComponent myprop={Myimage} />
like image 154
Lazar Nikolic Avatar answered Nov 21 '25 20:11

Lazar Nikolic



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!