Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

【IMPORTANT FOR EVERYONE】HOW TO FIX:Don't load new items when content height <= screen height #399

Open
othorizon opened this issue Mar 11, 2024 · 3 comments

Comments

@othorizon
Copy link

This is a known and explicit issue, and the author is no longer maintained.
It has also been mentioned in other issues how to fix it. ( #391 #380
If you don't intend to change other components or fork to fix bug, you can refer to my solution for fixing it.

import { useDebounceFn, useMount } from "ahooks";

export function useFixScroll(hasMore: boolean, fetchMore: () => void) {

// debounce is necessary
  const fixFetch = useDebounceFn(
    () => {
      const firstLoaderItem = document.querySelector(".loader-item");
      const scrollContainer = document.querySelector(".scroll-container");
      if (!firstLoaderItem || !scrollContainer) {
        return;
      }
      if (
        // only when user do not scroll content
       // The loading will continue only when the loader element appears on the scroll-container.
        scrollContainer.scrollTop === 0 &&
        firstLoaderItem.getBoundingClientRect().top <
          scrollContainer.getBoundingClientRect().bottom
      ) {
        // console.log("fixed");
        fetchMore();
      }
    },
    {
      wait: 500,
    },
  );
// useMount equals useEffect(()=>{doSomething();},[]); 
  useMount(() => {
    const observer = new MutationObserver((mutationsList) => {
      for (let mutation of mutationsList) {
        if (mutation.type === "childList") {
          // console.log("Child nodes have been added or removed.");
          fixFetch.run();
        }
      }
    });

    if (!hasMore) {
      return;
    }
    // scroll-items-container is the container for the items.
    const targetNode = document.getElementById("scroll-items-container");
    if (!targetNode) {
      return;
    }

    const config = { childList: true, subtree: false };
    // console.log("start observe");
    observer.observe(targetNode, config);
    return () => {
      observer.disconnect();
    };
  });
}
@othorizon othorizon changed the title 【IMPORT】HOW TO FIX:Don't load new items when content height <= screen height 【IMPORT FOR EVERYONE】HOW TO FIX:Don't load new items when content height <= screen height Mar 11, 2024
@hamzashakir99
Copy link

its any one found the solution, i have same problem and my project is near to complete, its dificult to change library

@othorizon
Copy link
Author

its any one found the solution, i have same problem and my project is near to complete, its dificult to change library

@hamzashakir99 What I mentioned above, that is my solution.just copy it and call this function

@othorizon othorizon changed the title 【IMPORT FOR EVERYONE】HOW TO FIX:Don't load new items when content height <= screen height 【IMPORTANT FOR EVERYONE】HOW TO FIX:Don't load new items when content height <= screen height Nov 10, 2024
@lawuysal
Copy link

its any one found the solution, i have same problem and my project is near to complete, its dificult to change library

@hamzashakir99 What I mentioned above, that is my solution.just copy it and call this function

Thanks for your solution. Where should I paste it and call it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants