Coder Social home page Coder Social logo

Comments (12)

mislavmiocevic avatar mislavmiocevic commented on June 26, 2024 5

@igorarkhipenko

Maybe this example will help you.
It was based on callback ref:
https://reactjs.org/docs/hooks-faq.html#how-can-i-measure-a-dom-node

Tested with React 17 and react-infinite-scroll-component 6.

import { render } from "react-dom";
import React, { useCallback, useState } from "react";
import InfiniteScroll from "react-infinite-scroll-component";

const InfiniteScrollComponent = () => {
  const [items, setItems] = useState(Array.from({ length: 30 }));

  const listItems = items.map((_, index) => <div key={index}>{index}</div>);

  const [listItemsContainerRef, setListItemsContainerRef] = useState();

  const onlistItemsContainerRefChange = useCallback((node) => {
    if (node !== null) {
      setListItemsContainerRef(node);
    }
  }, []);

  return (
    <div
      style={{
        height: "300px",
        // height: 'calc(100vh - 100px)', // this works also
        display: "flex",
        flexDirection: "column",
        background: "lightyellow"
      }}
    >
      <div style={{ padding: "20px", background: "lightblue" }}>Header</div>

      <div ref={onlistItemsContainerRefChange} style={{ overflow: "auto" }}>
        {listItemsContainerRef && (
          <InfiniteScroll
            dataLength={listItems.length}
            next={() => {
              window.setTimeout(() => {
                setItems((prevItems) => [
                  ...prevItems,
                  ...Array.from({ length: 30 })
                ]);
              }, 1500);
            }}
            hasMore={true}
            loader={<p style={{ color: "red" }}>...loading</p>}
            scrollableTarget={listItemsContainerRef}
          >
            {listItems}
          </InfiniteScroll>
        )}
      </div>
    </div>
  );
};

const App = () => {
  const [visible, setVisible] = useState(false); // faking modal show

  return (
    <div>
      <button onClick={() => setVisible(true)}>show</button>
      <button onClick={() => setVisible(false)}>hide</button>

      {visible && <InfiniteScrollComponent />}
    </div>
  );
};

render(<App />, document.getElementById("root"));

from react-infinite-scroll-component.

fabioimpe avatar fabioimpe commented on June 26, 2024 3

Thank you, in the meantime I found a workaround, I tried to set a fixed height to the InfiniteScroll component and it started to work also in the modals, in my case I set it as window.innerHeight, just because my modal is fullscreen.

from react-infinite-scroll-component.

ankeetmaini avatar ankeetmaini commented on June 26, 2024 3

from react-infinite-scroll-component.

AbdulMoiz96 avatar AbdulMoiz96 commented on June 26, 2024 3
import React, {useCallback, useState} from "react"
import InfiniteScroll from "react-infinite-scroller"
import {Table} from "antd"

const ScrollableTable = props => {
    
    const outerDivStyle = {height: '500px', overflow: 'auto'}
    const [tableContainerRef, setTableContainerRef] = useState()

    const onTableContainerRefChange = useCallback(node => {
        if (node !== null) {
            setTableContainerRef(node)
        }
    }, [])

    const fetchData = () => {
        // Fetch your data here. 
    }

return <div className="w-100" style={outerDivStyle} ref={onTableContainerRefChange}>
          <InfiniteScroll
              hasMore={true}
              initialLoad={false}
              loadMore={fetchData}
              useWindow={false}
              scrollableTarget={tableContainerRef}
          >
              <Table {...props}/> 
        </InfiniteScroll>
</div>

@martin-landhi, as @mislavmiocevic suggested, the above code works for me.

The additional thing I did, was just added useWindow={false}.

from react-infinite-scroll-component.

thudaohd avatar thudaohd commented on June 26, 2024 2

i have same issue. is there any solution?

from react-infinite-scroll-component.

atharhashmi avatar atharhashmi commented on June 26, 2024 1

@AbdulMoiz96 Awesome! This solved a major issue for us that we were struggling with for quite some time...

from react-infinite-scroll-component.

supuwoerc avatar supuwoerc commented on June 26, 2024 1

Although I passed height to fix the problem, strangely enough, my code executed normally in the development environment, build deployment to the production environment only found this error, I looked at the source code, against the window binding event to locate the error, too strange, I do not understand why the development environment is normal...
image

from react-infinite-scroll-component.

fabioimpe avatar fabioimpe commented on June 26, 2024

+1
I'm trying to use it in a modal component, but even passing the reference of the container with the scrollableTarget property, the next function is not triggered...
@ankeetmaini any help please?

from react-infinite-scroll-component.

ankeetmaini avatar ankeetmaini commented on June 26, 2024

from react-infinite-scroll-component.

igorarkhipenko avatar igorarkhipenko commented on June 26, 2024

@ankeetmaini Hey, did you ever find a solution for it? I'm having the same issue but the height of Modal component is not known before it gets rendered. I wonder if there's a way to use InfiniteScroll inside that in this case? Thanks.

from react-infinite-scroll-component.

martin-landhi avatar martin-landhi commented on June 26, 2024

@ankeetmaini coud you find a solution to that isssue?

from react-infinite-scroll-component.

supuwoerc avatar supuwoerc commented on June 26, 2024

Although I passed height to fix the problem, strangely enough, my code executed normally in the development environment, build deployment to the production environment only found this error, I looked at the source code, against the window binding event to locate the error, too strange, I do not understand why the development environment is normal...

I found the reason. It's something I did with the ui lib,ant-design/ant-design#43508

from react-infinite-scroll-component.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.