1

I want to load img in ReactJS. But I can't put url img in require.

enter image description here

enter image description here

Note: const 'Photo' is url link of picture.

Any help will be appreciated..

Thanks.

Salman Zafar
  • 3,844
  • 5
  • 20
  • 43

2 Answers2

0

You can require with expression.

So, this should work:

<img src={require('./upload/' + fileName)} />

The first part inside require is directory and the second one is fileName e.g. my-image.png.

Or there are various other ways to handle dynamic requires: map, switch

Ajeet Shah
  • 18,551
  • 8
  • 57
  • 87
  • I tried both of them didn't work. Anyway photo is really a correct path when I put "./pic.jpg" but string photo="./pic.jpg" is not work – Khiem Nguyen Apr 15 '20 at 10:13
0

This is the directory structure and 'photo' = "./upload/xxxxxxxxxx.jpg" enter image description here