Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

129
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda