Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

118
Views
Dynamic import/load of local images

I am working with a local JSON that has the following structure:

{
    "Data": [{
            "image": "../Images/project1.png",
        }, {
            "image": "../Images/project2.png",
        },
        {
            "image": ".../Images/project3.png",
        }
    ]
}

I am mapping this JSON into my component. I want it to show each image from the JSON. This is my component simplified:

import data from '../Data/Projects.json'

function ProjectsMenu(props){
    return(
        {data.Data.map(s =>{
            return(
                <img src={s["image"]} alt={s["image"]}></img>
            )
       })}  
}

This code doesn't load the images into the website. If I manually import the images it does work:

import Img from '../Images/present.png'
...
<img src={Img} alt={s["image"]}></img>

I want to be able to import them dynamially so that if the JSON is modified, I don't have to go back and add the image import to the code.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

It does not matter if yow pics are at Facebook, google or local host; it. Is a client/server architecture that uses the same protocol tcp/ip.

So as your are using cra the folder of yow pics need to be inside of your public folder, next to your index.html is

To fetch the images <img src=“/images/image-name.png” />

about 4 years ago · Juan Pablo Isaza Report

0

import data from '../Data/Projects.json'

function ProjectsMenu(props){
    return(
        {data.Data.map(s =>{
            return(
                <img src={require(s["image"])} alt={s["image"]}></img>
            )
       })}  
}

When we use dynamically then s["image"] will not load the image, we need to load the image manually by require(s["image"]).

If it did't work then try import(s["image"]).

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!