0

I dont know how to correct load path to img in react. When I do it this way everything works fine.

<img src = {require('../Images/komputer5.jpg')} alt = "productImage"></img>

But when I try to load this path from JSON i have error:

Uncaught Error: Cannot find module '../Images/komputer5.jpg'

Path in JSON

"image": "../Images/komputer5.jpg"

I tried to do it this way, but it didnt work

<img src = {require(product.image)} alt = "productImage"></img>

Can someone told me how do I get it to work in?

2
  • Can you explain about your directory architecture that you are using right now? Commented Oct 26, 2022 at 16:56
  • Src -> Images || src -> components -> products (from here I want to upload a photo) Commented Oct 26, 2022 at 16:59

1 Answer 1

2

change the json like this

"image": "komputer5.jpg"

then add the relative path

 require(`../Images/${product.image}`)
Sign up to request clarification or add additional context in comments.

Comments

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.