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

116
Vistas
react load grid of images based on name

I have the following images in a src/assets folder of a React project.

src/assests/
     a.webp
     b.webp
     c.webp

I would like to define a functional component that is a div containing the image corresponding to its name (name is a string).

I can get it to always be 'a'

import a from "./assets/a.webp";

function ImageTile({name}){
    return <div> 
               <img src = {a}/>
           </div>
}

But if I change it to this to try and be more general then the image doesn't load correctly.

function ImageTile({name}){
    return <div> 
               <img src = {eval(name)}/>
           </div>
}

Even if this did work it seems tedious to have to import every image. I'm happy to save the images somewhere else within the react app directory.

Here is a github gist with a full App.js example if it helps people understand what I am trying to do?

https://gist.github.com/oli5679/96f4549d82b1897a91c2528a4651cd51

I have also tried the approach suggested here, but get an error

function importAll(r) {
    let images = {};
    r.keys().map(item => { images[item.replace('./', '')] = r(item); });
    return images;
}

const images = importAll(require.context('./assets', false, '/\.webp/'));
console.log(images);
# require__(...).context is not a function
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