Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Broken Image React useState

I’m very Junior with ReactJS, I’m trying to fix a broken image, but just don’t figure out how to fix it.

This line of code should call the image, but does not work.

<img src= {product.image + ".png"} className="max-width-400" alt='product preview'/>

But, if I add the image route, it works.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

<img src= {"/img/products/" + product.image + ".png"} className="max-width-400" alt='product preview'/>

But, my teacher says that I need to create a constant or property to avoid show the /img/products/.

Thanks for your help.

>Solution :

Create (just after imports) constant field (export if needed) which holds base path to your images:

const BASE_PATH = "/img/products/";

Then use it everywhere like this:

<img src={BASE_PATH + product.image + ".png"} className=...
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading