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

290
Vistas
display images with forEach javascript

Images in the folder "images" aren't displayed in the view... the path is correct, browser don't display any error, can somebody help me please?

function Abstract() {

    let images = [...document.querySelectorAll('.img')];

    images.forEach((img, idx) => {
        img.style.backgroundImage = `url(../images/${idx+1}.jpeg)`
    })

    return (
       <div className="slider">
           <div className="slider-inner">

               <div className="item">
                   <div className="img">
                   </div>
               </div>

               <div className="item">
                   <div className="img">
                   </div>
               </div>

               <div className="item">
                   <div className="img">
                   </div> 
               </div>
               
           </div>
       </div>
    )
}
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Because of the way React works it's generally bad practice (there are exceptions, depending on your cause) to use native DOM methods to check and update the elements. It will tangle with the way that React manages state/DOM updates, and the result is a non-functioning component as you've found out.

Ideally you need an array of image names that you can map over to produce the JSX your component returns.

This is a working example based on your code but I've done four modifications.

  1. I've created an array of image names.

  2. I've created a parent component for Abstract, and passed the array from the parent into Abstract as a prop. (Note: this wasn't completely necessary, you could have declared the array in Abstract but this seemed more appropriate).

  3. In Abstract I map over the array to create an img for each element with a specific class based on its name (image image1 etc).

  4. I use CSS to define the background image information (I've used dummy images here).

const { useState } = React;

// Pass in the images array as a prop
// `map` over the array to create some images
// adding a new `className` to the image using the image name
function Abstract({ images }) {
  return (
    <div>
      {images.map(src => {
        const cn = `image ${src}`;
        return <img className={cn} />
      })}
    </div>
  );
}

const images = ['image1', 'image2', 'image3'];

// Pass in the images array as a prop to `Abstract`
function Example({ images }) {
  return <Abstract images={images} />
}

ReactDOM.render(
  <Example images={images} />,
  document.getElementById('react')
);
.image { width: 100px; height: 100px; }
.image1 { background-image: url('https://dummyimage.com/100x100/666/ff0'); }
.image2 { background-image: url('https://dummyimage.com/100x100/999/ff0'); }
.image3 { background-image: url('https://dummyimage.com/100x100/222/ff0'); }
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/17.0.2/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/17.0.2/umd/react-dom.production.min.js"></script>
<div id="react"></div>

about 4 years ago · Juan Pablo Isaza Denunciar

0

Maybe try :

let idx=1
return (
   <div class="slider">
       <div class="slider-inner">

           <div class="item">
               <div class="img" style="background-image: `url(../images/${idx++}.jpeg)`">
               </div>
           </div>

// repeat 3 times    
       
       </div>
   </div>
)
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