I have a list with a lot of components. When first loads, It shows 20 (index 0 - 19) components and when I scroll it will show more 20 components (index 0 - 39) and so on. Is it possible when I scroll It only show the components ( index 20 - 39 ) and then (index 40 - 59) and so on.
import React, {useState} from 'react';
import InfiniteScroll from "react-infinite-scroll-component";
import './App.css';
const style = {
height: 30,
border: "1px solid green",
margin: 6,
padding: 8
};
function App() {
const [items, setItems] = useState( Array.from({length: 20}));
const fetchMoreData = () => {
setTimeout(()=> {
setItems(items.concat(Array.from({length: 20})))
}, 1500);
};
return (
<div>
<div>
<InfiniteScroll
dataLength={items}
next={fetchMoreData}
hasMore={true}
loader={<h4>Loading...</h4>}
>
{items.map((i, index) => (
<div style={style} key={index}>
div - #{index}
</div>
))}
</InfiniteScroll>
</div>
</div>
);
}
export default App;
https://codesandbox.io/s/silly-silence-2rviq?file=/src/App.js:390-436