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? Commented Apr 14, 2020 at 15:04
  • 2
    If photo is the url, then there is no need for require, just use the constant directly. Commented Apr 14, 2020 at 15:08
  • When no need 'require' Its error: Cannot find module ' Commented Apr 15, 2020 at 10:17
  • How to dynamic require correctly? Commented May 29, 2020 at 22:16

2 Answers 2

0

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

Sign up to request clarification or add additional context in comments.

Comments

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 Comment

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

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.