Skip to content

How can i load a low-quality or low-res version before loading the full image? #308

Answered by igordanchenko
FunctionDJ asked this question in Q&A
Discussion options

You must be logged in to vote

@FunctionDJ, @Dzemik55

Here is a draft implementation illustrating one possible approach.

import Lightbox, { ImageSlide, isImageSlide, RenderSlideProps, SlideImage } from "yet-another-react-lightbox";
import Thumbnails from "yet-another-react-lightbox/plugins/thumbnails";

// ...

function CustomImageSlide({ slide, ...rest }: RenderSlideProps<SlideImage>) {
  const [preloaded, setPreloaded] = React.useState(false);
  const preloading = React.useRef<string>(undefined);

  React.useEffect(() => {
    preloading.current = slide.src;

    const image = new Image();
    image.onload = () => {
      if (preloading.current === slide.src) {
        setPreloaded(true);
      }
    };
    image.src = 

Replies: 1 comment 4 replies

Comment options

You must be logged in to vote
4 replies
@FunctionDJ
Comment options

@Dzemik55
Comment options

@FunctionDJ
Comment options

@igordanchenko
Comment options

Answer selected by igordanchenko
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants