ankeetmaini / react-infinite-scroll-component

An awesome Infinite Scroll component in react.
https://react-infinite-scroll-component.netlify.com/
MIT License
2.85k stars 322 forks source link

Infinite Scroll not working when using custom scrollbar #232

Open JilMuriel opened 3 years ago

JilMuriel commented 3 years ago

Hi, I really like this library.

I'm trying to use this library with a custom scrollbar but when scrolling at the bottom the function that loads more data does not work

https://codesandbox.io/s/infinite-scroll-w-custom-scrollbar-igdyk

App Component

import React, { useState } from "react";
import { Scrollbar } from "./Scrollbar";
import InfiniteScroll from "react-infinite-scroll-component";
import "./styles.css";

export default function App() {
  const [atpItems, setAtpItems] = useState(Array.from({ length: 20 }));

  const style = {
    height: 30,
    border: "1px solid green",
    margin: 6,
    padding: 8
  };
  const fetchMoreData = () => {
    // a fake async api call like which sends
    // 20 more records in 1.5 secs
    console.log("atpItems.lengthd", atpItems.length);
    if (atpItems.length <= 50) {
      setTimeout(() => {
        setAtpItems(atpItems.concat(Array.from({ length: 5 })));
      }, 1000);
    } else {
    }
  };
  return (
    <div className="App">
      <h1>Infinite Scroll</h1>
      <Scrollbar>
        <InfiniteScroll
          dataLength={atpItems.length}
          next={fetchMoreData}
          hasMore={true}
          loader={<h4>Loading...</h4>}
          endMessage={
            <p style={{ textAlign: "center" }}>
              <b>Yay! You have seen it all</b>
            </p>
          }
        >
          {atpItems.map((i, index) => (
            <div style={style} key={index}>
              div - #{index}
            </div>
          ))}
        </InfiniteScroll>
      </Scrollbar>
    </div>
  );
}

Scrollbar Component

import React from "react";
// import SimpleBar from 'simplebar-react';
import CustomScroll from "react-custom-scroll";
import "./Scrollbar.css";

interface ScrollBarProps {}

export const Scrollbar: React.FC<ScrollBarProps> = ({ children }) => {
  return (
    <CustomScroll allowOuterScroll heightRelativeToParent="400px">
      {children}
    </CustomScroll>
  );
};

What's the best way to handle this?

KristenWilde commented 3 years ago

I had this issue as well, but a few changes to the css fixed it. If I remember correctly, the important things were:

tomasz-grzesik commented 3 years ago

@JilMuriel Have you solve the problem?

paperfella-ceo commented 3 years ago

Same problem

JilMuriel commented 3 years ago

@tomasz-grzesik I was not able to solve It

gabe2code-opstalent commented 3 years ago

You should usemin-height: 30 instead of height. That solved the problem on my side