Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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;
      }
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading