react-infinite-scroll-component icon indicating copy to clipboard operation
react-infinite-scroll-component copied to clipboard

Unable to load more results when initial list loaded is smaller than container

Open jedster1111 opened this issue 3 years ago • 14 comments

Hi, really liking the library.

https://codesandbox.io/s/distracted-bogdan-zhcit?file=/src/index.js The above is a working example of inverse scrolling within a fixed height container of 300px.

If you modify the height (line 35) to 800px you'll see an initial load that looks like: Screenshot 2020-10-14 at 15 48 50

There doesn't seem to be any way to then trigger loading the next page of data.

Would this be something that would be possible to be addressed in this library? If there is no scrollbar, but there is still more data to fetch, then trigger the next function until there either is a scrollbar or there is no more data to fetch? Maybe you there is an alternative way to tackle this that I'm not thinking of as well.

Tested on

  • Chrome Version 86.0.4240.75
  • macOS Mojave Version 10.14.6

jedster1111 avatar Oct 14 '20 14:10 jedster1111

@jedster1111 I'm having the same issue, did you find a solution?

ADCrouch avatar Oct 26 '20 22:10 ADCrouch

@ADCrouch I ended up figuring something out, tried to recreate it in CodeSandbox. It's a little bit rough around the edges and I couldn't test the window resizing part but it should be a good enough starting point. https://codesandbox.io/s/vibrant-shannon-05o50?file=/src/Scroller.tsx

The logic is really just, if I'm not scrollable, there's still data to be loaded, and I'm not already loading, then load some more data. 😄

jedster1111 avatar Oct 27 '20 04:10 jedster1111

@jedster1111 What a great solution, thank you thank you thank you!

ADCrouch avatar Oct 27 '20 04:10 ADCrouch

same issue

sgehrman avatar Nov 12 '20 22:11 sgehrman

Also same issue.

compassmatt avatar Jan 28 '21 15:01 compassmatt

It would be super-nice if this logic were built in to react-infinite-scroll-component: If the element isn't scrollable but more items are available, request more items.

toschlog avatar Feb 19 '21 16:02 toschlog

Unfortunately, I have the same issue.

A workaround is to force Infinite Scroll to check the container height by triggering a custom scroll event on the window.

First, define the hook that triggers scroll events when deps change:

import { useEffect } from 'react';

export function useTriggerScrollFix(deps) {
  useEffect(() => {
    if (typeof window !== 'undefined') {
      window.dispatchEvent(new CustomEvent('scroll'));
    }
  }, deps);
}

Then invoke the hook inside the component that uses the infinite scroll:

function MyComponent() {
  // fetching logic of `items`....
  
 // Trigger manually a scroll event to
 // force infinite loading to reconsider
 // container height
 useTriggerScrollFix([items.length]);
  
 return (
   <InfiniteScroll
      dataLength={items.length}
      // ....
    >
      {items}
    </InfiniteScroll>
  );
}

panzerdp avatar May 06 '21 14:05 panzerdp

@panzerdp It seems this solution triggers infinite scroll to query for more results on load regardless of if the scroll bar is showing.

Correct solution should only trigger if there is no scrollable area. Otherwise might as well just increase the initial query amount (except in cases where that is not possible).

jacten avatar Aug 05 '21 20:08 jacten

same problem

symbiosdotwiki avatar Aug 19 '21 21:08 symbiosdotwiki

same problem

ssudekum avatar Aug 26 '21 23:08 ssudekum

same problem

sepehr500 avatar Sep 23 '21 23:09 sepehr500

Here is my jank solution (useInterval is from react-use)

useInterval(() => {
    const scrollComponent = document.getElementsByClassName(
      "infinite-scroll-component ",
    )[0];
    if (
      scrollComponent &&
      scrollComponent.scrollHeight < scrollComponent.clientHeight &&
      !isFetchingNextPage
    ) {
      fetchNextPage();
    }
  }, 1000);

sepehr500 avatar Sep 24 '21 01:09 sepehr500

still same problem

rafikor avatar Mar 22 '23 18:03 rafikor

Hey, did anyone find the fix for this. Having the same issue, when the size of the container doesn't allow scroll, I can still see my loader, but next() is not getting called.

@team kindly look into this!

Srezzx avatar Jun 14 '23 06:06 Srezzx