react-photo-gallery icon indicating copy to clipboard operation
react-photo-gallery copied to clipboard

Is there a way to lazy load images?

Open ArthurHwang opened this issue 5 years ago • 4 comments

Thank you for the awesome library, it has served me well.

However my gallery has ballooned to 30+ images. Is there a way to lazy load images? Or point me in the right direction?

I'm kind of clueless how I would implement it with this package.

ArthurHwang avatar Jun 09 '20 17:06 ArthurHwang

Did you have a look at ExampleDynamicLoading in the examples directory?

PeterUlb avatar Jun 09 '20 21:06 PeterUlb

I think the best approach is using "loading" attribute. Like this:

import Gallery from "react-photo-gallery";
import { photos } from "../../resources/photos";

export default function PhotoGallery() {
  useEffect(() => {
    photos.map(photo => {
      photo.loading = "lazy";
      return photo;
    });
  }, []);

  return <Gallery photos={photos} />
}

ako-v avatar Aug 21 '20 11:08 ako-v

I have used the react-lazy-load-image-component as followed: const imageRenderer = useCallback( ({ index, left, top, key, photo }) => ( <div className="image-overlay-container" style={{margin: '2px'}}> <LazyLoadImage alt={photo.alt} effect="blur" className="image-hover" width={photo.width} height={photo.height} placeholderSrc={photo.srcSet.lazyLoadPlaceholder} src={photo.srcSet.thumbnail} style={{ top: top, left: left}} /> <div className="overlay text-center" onClick={(e) => { openLightbox(e, { photo, index }) }}> <p>{photo.alt}</p> <img alt="Eye SVG" src='/api/temp/eye.svg' /> </div> </div> ) );

<Gallery2 photos={selectedWork} renderImage={imageRenderer}/> <ModalGateway> {viewerIsOpen ? ( <Modal onClose={closeLightbox}> <Carousel currentIndex={currentImage} views={selectedWork.map(x => ({ ...x, srcset: x.srcSet, caption: x.alt }))} /> </Modal> ) : null} </ModalGateway>

I don't know why my formatting is broken

mattg66 avatar Dec 24 '20 21:12 mattg66

formatted, for future generations ;)

const imageRenderer = useCallback(({index, left, top, key, photo}) => (
        <div className="image-overlay-container" style={{margin: '2px'}}>
            <LazyLoadImage
                alt={photo.alt} effect="blur" className="image-hover" width={photo.width} height={photo.height} placeholderSrc={photo.srcSet.lazyLoadPlaceholder}
                src={photo.srcSet.thumbnail} style={{top: top, left: left}}
            />
            <div className="overlay text-center" onClick={(e) => {
                openLightbox(e, {photo, index})
            }}>
                <p>{photo.alt}</p>
                <img alt="Eye SVG" src='/api/temp/eye.svg'/>
            </div>
        </div>
    )
);

<Gallery2 photos={selectedWork} renderImage={imageRenderer}/>
<ModalGateway> {
    viewerIsOpen
        ? (<Modal onClose={closeLightbox}>
            <Carousel currentIndex={currentImage} views={selectedWork.map(x => ({...x, srcset: x.srcSet, caption: x.alt}))}/>
        </Modal>)
        : null
}
</ModalGateway>

ReallyLiri avatar May 21 '21 09:05 ReallyLiri