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

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

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

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 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