Comments (12)
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.
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.
from react-infinite-scroll-component.
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.
i have same issue. is there any solution?
from react-infinite-scroll-component.
@AbdulMoiz96 Awesome! This solved a major issue for us that we were struggling with for quite some time...
from react-infinite-scroll-component.
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...
from react-infinite-scroll-component.
+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.
from react-infinite-scroll-component.
@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.
@ankeetmaini coud you find a solution to that isssue?
from react-infinite-scroll-component.
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)
- Integration with Headless UI popover not working as expected.
- Block is appearing during scroll HOT 1
- Number 0 appears when finish loading message appears HOT 1
- `scrollableTarget` throws error `Type 'HTMLElement' is not assignable to type 'ReactNode'` in React 18 HOT 3
- Inverse does not work if you do not set a specific height
- Not calling next function on long screens HOT 13
- can not working in shadowDOM
- react-infinite-scroll-component is no longer maintained HOT 3
- The component is not working after a certain amount of items in the list
- Don't loading new content, if initial content height <= screen height HOT 4
- Please modify the npm docs and mention that for the dataLength prop we need to pass the actual length of the data that we are currently rendering.
- TypeError on latest Next.js HOT 4
- Bidirectional Infinite Scroll HOT 2
- Wrong calculation in isElementAtTop method causing percentage scrollThreshold to fail in inverse mode HOT 1
- Invalid Hook call on SetProgress -> 100%
- is this library supported horizontal scroll?
- 【IMPORT FOR EVERYONE】HOW TO FIX:Don't load new items when content height <= screen height
- Possibility to pass throttle time as props
- How can I change the scrollY when the InfiniteScroll has rendered HOT 1
- next function doesn't called HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from react-infinite-scroll-component.