0

I wanted to import image in react without declaring it on top of the file like, import img from './this/is/file.png'

But, I wanted to import it directly to the jsx tag, how can I do that?

I've tried <img src={import './this/is/file.png'} alt='' /> but it does not work.

Roby Cigar
  • 766
  • 2
  • 14
  • 28
  • https://stackoverflow.com/questions/44154939/load-local-images-in-react-js, https://stackoverflow.com/questions/50844727/importing-images-locally-in-react – Dennis Vash Feb 06 '21 at 09:13

1 Answers1

0

Use require()

<img src={require( './this/is/file.png')} alt='' />

Thinking about it though, I'm not sure the bundler (webpack?) will know what to do with this. There is also the potential for dynamic imports to help.

spirift
  • 2,994
  • 1
  • 13
  • 18