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

531
Vistas
React, cannot import local images inside an array to loop over

I am learning React.js and aiming to make a product page, and I have some issues with importing images from my local directory. These are from my actual project, but it's an online editor, but it should be fine because it's the same issue. These are my files:

enter image description here

My App.js:

import "./styles.css";
import Images from "./Images";

export default function App() {
  return (
    <div className="container">
      {Images.map((content) => (
        <div className="product-image">
          <img src={content.image} />
        </div>
      ))}
    </div>
  );
}

My Images.js:

const Images = [
  {
    id: 1,
    image: "./img/img1.jpg"
  },
  {
    id: 2,
    image: "./img/img2.jpg"
  },
  {
    id: 3,
    image: "./img/img3.jpg"
  }
];

export default Images;

For Image.js, It is weird because if I make the image source to a url format, it works fine. The local directory name shouldn't be a problem because that's where I imported other sources in my project. Any helps would be appreciated.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

For this to work, you have two choices, either you put your img folder inside public folder and change Images.js to this:

const Images = [
  {
    id: 1,
    image: "/img/img1.jpg"
  },
  {
    id: 2,
    image: "/img/img2.jpg"
  },
  {
    id: 3,
    image: "/img/img3.jpg"
  }
];

export default Images;

Or you keep img folder inside src and change Images.js to this:

import img1 from "./img/img1.jpg";
import img2 from "./img/img2.jpg";
import img3 from "./img/img3.jpg";
const Images = [
  {
    id: 1,
    image: img1
  },
  {
    id: 2,
    image: img2
  },
  {
    id: 3,
    image: img3
  }
];

export default Images;

See this Stack Overflow QA to know why it's like this.

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