I tried to use react-infinite-scroll-component
to get an infinite scrolling effect.
However, the arr
is updating but updating as full length array at the end.
And, I want it to update as slicing by 10 (like first 10, then 10, …) till length of array using that package. So, array should be updated by length 10 only every 5 secs.
Note: I found below two stackoverflow links which was so messing me a lot!!
- Whats the best way to update an object in an array in ReactJS?
- Correct modification of state arrays in React.js
And, I couldn’t understand anything (and they also use class component well I know how to implement it into functional but I believe something is wrong while mutating array!)
Here is the live demo: codesandbox link
Any suggestions, what is the problem?
2
Answers
I guess the issue is that the
first10
state variable is not being updated correctly inside thesetTimeout
callback function. I changed a few things in your code. Check if the following works for you as intended:Try this:
DataScroller
component that takes the data and a size i.e. 15arr
variabel inutils
to dynamically generate 120 recordsfetchMoreData
is now a callbackhasMore
upon data changesNote: I updated
react-infinite-scroll-component
to6.1.0
(latest version available as of this post). I was getting a lot of Warning with the version you were using.App.js
DataScroller.js
utils.js