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

234
Vistas
Require statement doesn't work as expected?

I want to show some images depending on how many I get through an array, so I can't really use an import statement.

I googled and I had found that I could just use require(), but it doesn't work as expected.

GameImage.tsx

import './css/GameImage.css';

type Image = {
    imgSrc: string,
    imgAlt: string
}

type Images = {
    images: Array<Image>
}

function GameImage(props: Images) {
    return (
    <div className="game-details-image-wrapper">
        {props.images.map((pair) => (
            <img className="game-details-image" src={require(pair.imgSrc).default} alt={pair.imgAlt} />
        ))}  
    </div>);
}

export default GameImage;

how I render it:

<GameImage images={[{imgSrc: './img/Untitled.png', imgAlt: 'test'}]}/>

This way, I will get

Cannot find module './img/Untitled.png'

but if I modify the img tag like this:

<img className="game-details-image" src={require("./img/Untitled.png").default} alt={pair.imgAlt} />

it works properly.

Why does it happen and how can I fix it?

about 4 years ago · Juan Pablo Isaza
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