hotcakedev
hotcakedev

Reputation: 2504

React lazy load won't work inside React perfect scroll bar

I am using react-perfect-scrollbar to show images list. Inside the perfect scroll bar, I am going to lazy load images. But it won't work.

import React, { useState, useCallback, useEffect } from 'react';
import PerfectScrollbar from 'react-perfect-scrollbar';
import useIsMountedRef from 'src/hooks/useIsMountedRef';
import LazyLoad from 'react-lazyload';
import {
  Box,
  Button,
  Link,
  List,
  ListItem,
  ListItemIcon,
  ListItemText,
  CircularProgress,
  Typography,
  makeStyles
} from '@material-ui/core';
  const isMountedRef = useIsMountedRef();
  const [images, setImages] = useState([]);

  const getImages = useCallback(async () => {
      try {
        setLoading(true);
        const response = await axios.get(`${backendUrl}/images/get/images`);

        if (isMountedRef.current) {
          setImages(response.data.projectImages);
        }
      } catch (err) {
        console.error(err);
      } finally {
        if (isMountedRef.current) {
          setLoading(false);
        }
      }
  }, [isMountedRef]);

          <PerfectScrollbar options={{ suppressScrollX: true }}>
            <List className={classes.list}>
              {images.map((image, i) => (
                <ListItem
                  divider={i < images.length - 1}
                  key={i}
                  className={classes.listItem}
                >
                  <ListItemIcon>
                    <LazyLoad height={90} key={i} overflow>
                      <img
                        src={`${awsS3Url}/${image.Key}`}
                        className={classes.listImage}
                        onClick={() => onSelect(`${awsS3Url}/${image.Key}`)}
                      />
                    </LazyLoad>
                  </ListItemIcon>
                  <ListItemText
                    primary={GetFilename(image.Key)}
                    primaryTypographyProps={{ variant: 'h5' }}
                    secondary={bytesToSize(image.Size)}
                    className={classes.listItemText}
                  />
                    <MoreButton
                      handleArchive={() => handleRemoveOne(image)}
                    />
                </ListItem>
              ))}
            </List>
          </PerfectScrollbar>

Some of images(the first view without scrolling) are showing. When I scroll, the images won't load, only the list contents without images are showing.

What did I code wrong?

Upvotes: 1

Views: 2325

Answers (1)

Joonas Mankki
Joonas Mankki

Reputation: 11

I had the same problem. The issue is that react-lazyload is trying to find a container that has its overflow property set to scroll or auto, but perfect-scrollbar sets its overflow property to hidden and handles the scrolling manually instead. So we have to tell react-lazyload manually which container it should monitor for scroll events.

This can, by documentation, be done in two ways; by passing an HTMLElement or a query selector string. Alas, there seems to be a bug in the library that causes the property to be ignored if it is not a string (https://github.com/twobin/react-lazyload/blob/055405125d0313014f0951cffc78345297f10a08/lib/index.js#L261) so currently the only way is to pass a query selector string.

But when I tried to pass a query selector string targeting the perfect-scrollbars container, it seems that the container might not always be there yet when react-lazyload attaches its event listeners, so we must check that the container is actually there before we initialize the LazyLoad-container.

So the relevant code is:

import React, { ReactElement, useRef, useState, useEffect } from 'react';
import PerfectScrollbar from 'react-perfect-scrollbar'
import LazyLoad from 'react-lazyload';

export default (): ReactElement => {

  // Get a reference to the wrapper element so we know when it is created
  const scrollbarWrapperRef = useRef(null);

  // Initialize a state setter to notify the view when the scrollParent becomes available
  const [scrollParent, setScrollParent] = useState<HTMLElement|null>(null);

  // Adjust this selector to your liking
  const scrollParentSelector = '#scrollbar-wrapper .scrollbar-container';

  // Here we make sure that the PerfectScrollbar container is actually available before we let the content and the LazyLoads be created.
  useEffect(() => {
    const scrollParentElement = document.querySelector(scrollParentSelector);
    if (scrollParentElement) {
      setScrollParent(scrollParentElement);
    }
  }, [scrollbarWrapperRef.current]);

  // The relevant DOM
  return (
    <div ref={scrollbarWrapperRef} id="scrollbar-wrapper">
      <PerfectScrollbar>
        { scrollParent &&
        <List>
          {images.map((image, i) => (
            <LazyLoad scrollContainer={scrollParentSelector}>
              <img src="..." />
            </LazyLoad>
          ))}
        </List>
        }
      </PerfectScrollbar>
    </div>
  );
}

Upvotes: 1

Related Questions