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

React Display image from database in a Table

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:

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

[{"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?

>Solution :

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.

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