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

109
Visualizações
Add additional props to component in a foreach loop

i have a component in React that receives some elements (photos), i have a object with this composition:

getItems() {
    const { gallery } = this.props;
    const { content_elements: contentElements } = gallery;
    const items = [];

    if (contentElements) {
      contentElements.forEach((photo, index) => {
        let item = (
          <div key={photo.id} className="gal__fig-item">
            <Image
              srcset="gallery"
              figureClassName="mm"
              imgClassName="mm__img"
              noZoom={true}
              showCaption={false}
              wrapperClassName="mm__wr"
              {...photo}/>
          </div>
        );           

        items.push(item);
      });
    }

    return items;
  }

I need to add a prop in the image component, but only in the first element (index = 0).

I do this:

getItems() {
    const { gallery } = this.props;
    const { content_elements: contentElements } = gallery;
    const items = [];

    if (contentElements) {
      contentElements.forEach((photo, index) => {
        let item = (
          <div key={photo.id} className="gal__fig-item">
            <Image
              srcset="gallery"
              figureClassName="mm"
              imgClassName="mm__img"
              noZoom={true}
              showCaption={false}
              wrapperClassName="mm__wr"
              {...photo}/>
          </div>
        );
        if(index === 0){
           item = (
            <div key={photo.id} className="gal__fig-item">
              <Image
                srcset="gallery"
                figureClassName="mm"
                imgClassName="mm__img"
                noZoom={true}
                showCaption={false}
                hasLazyLoad={false}
                wrapperClassName="mm__wr"
                {...photo}/>
            </div>
          );
        }

        items.push(item);
      });
    }

    return items;
  }

That works, but....maybe there is an other option, more clean code, to do it? Someone can help me? Thanks

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

you can set hasLazyLoad based on the value of i:

 if (contentElements) {
      contentElements.forEach((photo, index) => {
        let item = (
            <div key={photo.id} className="gal__fig-item">
              <Image
                srcset="gallery"
                figureClassName="mm"
                imgClassName="mm__img"
                noZoom={true}
                showCaption={false}
                hasLazyLoad={i !== 0} // use i !== 0 to false when i = 0
                wrapperClassName="mm__wr"
                {...photo}/>
            </div>
          );
        }
    ```
about 4 years ago · Juan Pablo Isaza Relatório

0

I would replace forEach to map and add ternary operator for hasLazoLoad prop:

getItems() {
  const { gallery } = this.props;
  const { content_elements: contentElements } = gallery;

  const items = contentElements?.map((photo, index) => {
    return <div key={photo.id} className="gal__fig-item">
      <Image
        srcset="gallery"
        figureClassName="mm"
        imgClassName="mm__img"
        noZoom={true}
        showCaption={false}
        hasLazyLoad={index === 0 ? false : true}
        wrapperClassName="mm__wr"
        {...photo} />
    </div>
  }) || [];

  return items;
}
about 4 years ago · Juan Pablo Isaza Relatório

0

I suggest this :

if index was zero then hasLazyload will be false and vise versa

getItems() {
        const { gallery } = this.props;
        const { content_elements: contentElements } = gallery;
        const items = [];
    
        if (contentElements) {
          contentElements.forEach((photo, index) => {
            let item = (
              <div key={photo.id} className="gal__fig-item">
                <Image
                  srcset="gallery"
                  figureClassName="mm"
                  imgClassName="mm__img"
                  noZoom={true}
                  showCaption={false}
                  hasLazyLoad = {index !== 0}  
                  wrapperClassName="mm__wr"
                  {...photo}/>
              </div>
            );           
    
            items.push(item);
          });
        }
    
        return items;
      }
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