Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

101
Views
Agregue accesorios adicionales al componente en un bucle foreach

tengo un componente en React que recibe algunos elementos (fotos), tengo un objeto con esta composición:

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

Necesito agregar un accesorio en el componente de la imagen, pero solo en el primer elemento (índice = 0).

Hago esto:

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

Eso funciona, pero... ¿quizás haya otra opción, un código más limpio, para hacerlo? ¿Puede ayudarme alguien? Gracias

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

puede establecer hasLazyLoad en función del valor de 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 Report

0

Reemplazaría forEach para mapear y agregar un operador ternario para 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 Report

0

Sugiero esto:

si el índice era cero, hasLazyload será falso y viceversa

 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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!