react-infinite-scroll-component icon indicating copy to clipboard operation
react-infinite-scroll-component copied to clipboard

hasMore is true, but loadMore only gets called twice

Open girlslearningcodeyul opened this issue 4 years ago • 17 comments
trafficstars

Even if hasMore is set to true (to test this I have hard set it to always be true), loadMore function gets called only twice. Has anyone seen this type of issue before?

                               <InfiniteScroll
                                dataLength={eventListCount}
                                next={this.loadMore}
                                hasMore={!isFetching && hasMore}
                                loader={<h4>Loading...</h4>}
                                height={height}
                                >
                              //  .. render component which lists data from eventList
                            </InfiniteScroll>

girlslearningcodeyul avatar May 14 '21 16:05 girlslearningcodeyul

same issue I am also facing but in my case lodemore is called only once

 ` <InfiniteScroll
               className={classes.InfiniteScrolls}
               dataLength={totalLength} //This is important field to render the next data
               next={fetchData}
               hasMore={items.length >= totalLength ? false : true}
    >
             `

RAMKUMARDANE avatar May 18 '21 11:05 RAMKUMARDANE

I had the same issue. It turns out I was using dataLength wrong. I thought it was supposed to be the total length of items that could be displayed, but instead it seems it should be the length of items that are currently displayed, i.e. in @RAMKUMARDANE's example above it should probably be dataLength={items.length}...

TBH I am not sure why the component needs dataLength given it uses hasMore to know if there's more to render, and it could presumably use children.length to know how many elements are displayed. 🤷

mikelehen avatar May 23 '21 21:05 mikelehen

I had the same issue. It turns out I was using dataLength wrong. I thought it was supposed to be the total length of items that could be displayed, but instead it seems it should be the length of items that are currently displayed, i.e. in @RAMKUMARDANE's example above it should probably be dataLength={items.length}...

TBH I am not sure why the component needs dataLength given it uses hasMore to know if there's more to render, and it could presumably use children.length to know how many elements are displayed. 🤷

hmmm, thank you @mikelehen . I tried setting dataLength to the number of items that a server is supposed to return (which is 20 in this case) and hasMore still only ran twice, even though to troubleshoot I hard-coded hasMore to true.

girlslearningcodeyul avatar May 24 '21 16:05 girlslearningcodeyul

@girlslearningcodeyul Ah, bummer. You may have a different issue than me then. In my case I was setting dataLength to be a number much greater than the number of children elements I was rendering inside the InfiniteScroll tag and so I think it was essentially waiting for me to add the rest of the children elements (i.e. it thought I was still asynchronously fetching data or something).

I'm not sure what the issue is in your case. Though I could imagine that just setting it to 20 wouldn't work because after you get to the 2nd page, there should be 40 elements being rendered, not 20. If it's easy to share a demo of your example that's not working it might be possible to help debug. Else, I ended up solving my issue by comparing what I was doing to the live examples linked from the github readme...

mikelehen avatar May 24 '21 17:05 mikelehen

Correction! Thank you @mikelehen . I figured what it was, but not without your help. Looking at dataLength, the issue is, it has to to change, so moving it to the reducer and instead of adding up the number of items to render, substract from the total length count. Works like magic now!

girlslearningcodeyul avatar May 24 '21 17:05 girlslearningcodeyul

Correction! Thank you @mikelehen . I figured what it was, but not without your help. Looking at dataLength, the issue is, it has to to change, so moving it to the reducer and instead of adding up the number of items to render, substract from the total length count. Works like magic now!

for example, I have 48 items in total, the server returns 20 at a time. The first load dataLength must be 48, on second that value should change to 28 and then just 8.

girlslearningcodeyul avatar May 24 '21 17:05 girlslearningcodeyul

@girlslearningcodeyul Yay! 🎉 Glad it's working.

Strangely, I ended up doing something different. If I had 48 items, then on first load, I'd set dataLength to 20, and then on second 40, and then 48 (with hasMore=false at that point). Maybe it works both ways. 🤷

mikelehen avatar May 24 '21 17:05 mikelehen

Hey, I also met it and fix for me: set to dataLength a current length of list (from state). It will a trigger for calling fetchMore. I tried set total list of data - doesn't work.

sychovSaveliy avatar Jun 06 '21 13:06 sychovSaveliy

same issue I am also facing but in my case lodemore is called only once

 ` <InfiniteScroll
               className={classes.InfiniteScrolls}
               dataLength={totalLength} //This is important field to render the next data
               next={fetchData}
               hasMore={items.length >= totalLength ? false : true}
    >
             `

// import InfiniteScroll from "react-infinite-scroll-component"; import InfiniteScroll from "react-infinite-scroller";

I changed liberary as suggested by another developer. and infinite Scroll with required changes works.

RAMKUMARDANE avatar Jun 09 '21 04:06 RAMKUMARDANE

this should be updated in the documentation, fixed as the comment above

JFernandoGomez avatar Aug 06 '21 09:08 JFernandoGomez

I had the same issue. It turns out I was using dataLength wrong. I thought it was supposed to be the total length of items that could be displayed, but instead it seems it should be the length of items that are currently displayed, i.e. in @RAMKUMARDANE's example above it should probably be dataLength={items.length}...

TBH I am not sure why the component needs dataLength given it uses hasMore to know if there's more to render, and it could presumably use children.length to know how many elements are displayed. 🤷

You've saved my day!

dohoangtung avatar Nov 22 '21 15:11 dohoangtung

I had the same issue. It turns out I was using dataLength wrong. I thought it was supposed to be the total length of items that could be displayed, but instead it seems it should be the length of items that are currently displayed, i.e. in @RAMKUMARDANE's example above it should probably be dataLength={items.length}...

TBH I am not sure why the component needs dataLength given it uses hasMore to know if there's more to render, and it could presumably use children.length to know how many elements are displayed. 🤷

You made my day man! If you feel comfortable send me your BTC wallet address. Would be a pleasure to me to buy you a coffee ☕️

nardocesar avatar Feb 02 '22 13:02 nardocesar

@nardocesar Hah, glad it helped but no need to buy me coffee! Feel free to send that goodwill towards somebody else you run across who could use it. Have a great day!

mikelehen avatar Feb 07 '22 16:02 mikelehen

I checked dataLength and also hasMore but I figured out that it still makes twice calls if the page height changes (in my case)! so if you have a similar problem it's better to set min-height to list container.

arensade avatar Feb 22 '22 18:02 arensade

Hey, I also met it and fix for me: set to dataLength a current length of list (from state). It will a trigger for calling fetchMore. I tried set total list of data - doesn't work.

Thanks @sychovSaveliy, you're right. This solved the issue I was having

wisnuvb avatar Aug 17 '22 04:08 wisnuvb

I had the same issue. It turns out I was using dataLength wrong. I thought it was supposed to be the total length of items that could be displayed, but instead it seems it should be the length of items that are currently displayed, i.e. in @RAMKUMARDANE's example above it should probably be dataLength={items.length}...

TBH I am not sure why the component needs dataLength given it uses hasMore to know if there's more to render, and it could presumably use children.length to know how many elements are displayed. 🤷

Thank you very much. This worked perfectly for me. Thanks again!

MusabDev avatar Oct 07 '22 05:10 MusabDev

https://stackoverflow.com/questions/67699376/react-infinite-scroll-component-stopped-working-after-one-call-loadmore-only

carlos-dubon avatar Feb 01 '24 23:02 carlos-dubon