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.

4
  • What is the directory structure and what is location of image? Apr 14, 2020 at 15:04
  • 2
    If photo is the url, then there is no need for require, just use the constant directly.
    – Chris
    Apr 14, 2020 at 15:08
  • When no need 'require' Its error: Cannot find module ' Apr 15, 2020 at 10:17
  • How to dynamic require correctly?
    – Ajeet Shah
    May 29, 2020 at 22:16

2 Answers 2

0

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

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

1
  • 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 Apr 15, 2020 at 10:13

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.