1

I am trying to get an image from the database in a table. The database contains the image name, the images are stored locally in /src/media The script is in /src/components

For now i am using a mock_data.json file instead of the actual database.

The JSON:

[{"id":1,"Image":"img1.jpg","Cr":"1/2","Name":"Goblin","Source":"Monster 
Manual","Type":"Humanoid","Size":"Medium","Alignment":"N","Tag":"tag","Info":"Info"},
{"id":2,"Image":"img2.jpg","Cr":"1/4","Name":"Spider","Source":"Monster 
Manual","Type":"Monster","Size":"Medium","Alignment":"N","Tag":"tag","Info":"Info"}
]

The SCRIPT:

import "./Monsters.css";
import data from "./mock_data.json";
import { useState } from "react";

export default function Monsters() {

const [monsters, setMonsters] = useState(data);
return (
         
    <div className="app-container">
        <h1>Monsters</h1>
        <table>
            <thead>
                <tr>
                    <th> </th>
                    <th>Cr</th>
                    <th>Name</th>
                    <th>Source</th>
                    <th>Type</th>
                    <th>Size</th>
                    <th>Alignment</th>
                    <th>Tag</th>
                    <th>Info</th>
                </tr>
            </thead>
            <tbody>
                {monsters.map((monster) => (
                    <tr>
                       <td><img src={`../media/${monster.Image}`} alt={monster.Name} width="25" /></td>
                        <td>{monster.Cr}</td>
                        <td>{monster.Name}</td>
                        <td>{monster.Source}</td>
                        <td>{monster.Type}</td>
                        <td>{monster.Size}</td>
                        <td>{monster.Alignment}</td>
                        <td>{monster.Tag}</td>
                        <td>{monster.Info}</td>
                    </tr>
                ))}
                
            </tbody>
        </table>
    </div>
    
)
};

If I inspect the webpage, I can see the name and location are correct

<img src="../media/img2.jpg" alt="Spider" width="25">

The image is not showing, the alt text is.

enter image description here

What am I doing wrong?

3
  • Is your image is stored in your folder structure? Commented Feb 13, 2023 at 11:08
  • create-react-app.dev/docs/adding-images-fonts-and-files Commented Feb 13, 2023 at 11:11
  • the images are stored locally in /src/media (img1.jpg and img2.jpg) - i also tried to put them in the components directory and edited the code, but same problem. Did not show Commented Feb 13, 2023 at 11:11

3 Answers 3

2

To display images in /src/media then you can use require() like so:

<td><img src={require(`../media/${monster.Image}`)} alt={monster.Name} width="25" /></td>

This tells React you're referencing a local image which is out of the public folder, and it will be included in the build bundle.

The difference between this and putting images in public folder is this way, React bundles the images with the build number in the filename e.g. /static/media/img1.5c00184d3c036ea8ce95.jpeg, which changes with updates. This means the browser will not be stuck with an old/stale image.

If you use the public folder, and you update an image, the browser may load the old image from cache. The browser will simply see /img1.png as the src.

Another difference is using images inside src and require(), React will trigger errors for missing images, whereas in the public folder will not.

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

2 Comments

Nice, also works, so i got 2 answers that i could use: The one from Eduardo Motta de Moraes and yours. Since it doesn't change my structure i think this is the one i will use. So if i got it correct, if images are in the public folder, you can call them without the require and if i put them anywhere outside the public folder, i need to use require to show them.
Correct :) see my update for the differences.
2

Assuming you're using create-react-app, if you move your image files to the Public folder, you should be able to reference them with the public folder env variable:

<th><img src={`${process.env.PUBLIC_URL}/media/${monster.Image}`} alt={monster.Name} width="25" /></th>

1 Comment

Thanks! That actually worked. But can not stop to wonder what is so different about your solution. The only thing that is changed was moving the media directory from the src directory to the public directory....?
0

The issue you are facing is due to CSS. Add height in to it you will get the desire result and it should be table data

 <tr>
   <td><img src={`../media/${monster.Image}`} alt={monster.Name} 
        height="25" width="25" />
  </td>
</tr>

1 Comment

Thanks for the reply, stupid! it has to be <td> I changed that. About the CSS, i added height, but it still don't show the image <img src="../media/img2.jpg" alt="Spider" height="25" width="25">

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.