Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

124
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda