react-virtuoso icon indicating copy to clipboard operation
react-virtuoso copied to clipboard

Reset endReached

Open WillSquire opened this issue 4 years ago • 30 comments

Using the infinite loader implementation, if I scroll to the end of the list and then change something to retrieve a newly filtered or sorted list, then it may or may not load more if I scroll to the bottom of the list again. I'm not sure if this has something to do with the count being used to determine if the bottom of a list has been reached and then not being triggered again if it doesn't change?

WillSquire avatar Oct 19 '20 17:10 WillSquire

Hey,

endReached is triggered only once when a certain index is reached - so yes, if the total count does not change, you won't get notified about it. This made sense for the use case I had in mind, but it might not work for another one. Can you post an example so that I can take a look? Thanks.

petyosi avatar Oct 20 '20 05:10 petyosi

Hi @petyosi ,

Ah, that sounds like the case. I've dissected some of the code to be as minimal as possible, but I'm not sure how useful it will be. If I change sort it will probably be the same sized list if it's cached from previous. apollo can serve cached data on the next render so it'll remain the same without a break (i.e. without setting it to [] or null)

const {
  data,
} = useQuery({
  variables: {
    sort,
  },
})

<Virtuoso
  overscan={500}
  item={Item}
  endReached={onLoadMore}
  totalCount={data?.length}
/>

Hope this helps?

WillSquire avatar Oct 20 '20 08:10 WillSquire

@petyosi Just to give a bit more info, I think this is relevant:

  • Apollo first shows cached 'stale' data (let's say it's 6 items long)
  • Apollo can then return 'new' data (let's say the pagination is set to 5 items long)

This would come back as less than the first and would still require another page to be loaded.

WillSquire avatar Nov 03 '20 13:11 WillSquire

Thank you - I need to think this through. In the case you describe, the endReached should be reset - but I am not sure how to do this in a predictable manner. You can try atBottomStateChange event in the meantime.

petyosi avatar Nov 03 '20 13:11 petyosi

Thanks, I'll give that's a try! Not sure if this is applicable, but would an array comparison help in this situation? (i.e. oldData !== newData or useMemo)

WillSquire avatar Nov 03 '20 13:11 WillSquire

+1 I have run into the same issue.

My use case: a list of items in a marketplace. When adding keywords in the searchbar, the items in the list are replaced, but the item count doesn't change (except if the users has already scrolled, but by default it doesn't).

jiminy-billy-bob avatar Nov 13 '20 07:11 jiminy-billy-bob

+1 I also run into same issue. So i am using it to make kind of endless scrolling grid, which fetches more data over the api, after reaching the end. But if, due to some error, api doesn't give new data or maybe due to some network error( as user lost connection in between), the data count remains same, but when, connection again gets established and user tries to load more data by reaching end, the onEndReached does not fire again And further atBottomStateChange is not available in VirtuosoGrid

ArjobanSingh avatar Nov 18 '20 09:11 ArjobanSingh

@WillSquire @jiminy-billy-bob @ArjobanSingh Would exposing a resetEndReached() method work for you? I can't think of any reasonable way to detect the logic flow you refer to. Please 👍/👎.

petyosi avatar Nov 18 '20 11:11 petyosi

Yes, that would totally be usable.

jiminy-billy-bob avatar Nov 18 '20 11:11 jiminy-billy-bob

@WillSquire @jiminy-billy-bob @ArjobanSingh Would exposing a resetEndReached() method work for you? I can't think of any reasonable way to detect the logic flow you refer to. Please /.

i guess, if on resetting end reached, virtuoso grid fires onEndReached again on reaching end, than ofcourse, this method will work.

ArjobanSingh avatar Nov 18 '20 11:11 ArjobanSingh

or is there a way, where we can attach onScroll method, which will give us offset in params - like normal onScroll in react element, this way we can count when offsettop is 0, and invoke our load more function.

ArjobanSingh avatar Nov 18 '20 11:11 ArjobanSingh

@ArjobanSingh - onScroll callback is coming. In fact, it is already working in v0.21.0-alpha.3 which I am promoting for an early feedback.

petyosi avatar Nov 18 '20 12:11 petyosi

cool, is alpha version released for usage?

ArjobanSingh avatar Nov 18 '20 14:11 ArjobanSingh

@ArjobanSingh Yes, It is. Notice that there are certain deprecated properties, so watch your console for warnings.

petyosi avatar Nov 18 '20 15:11 petyosi

Ok thanks man, is it safe to use alpha version on production now?

ArjobanSingh avatar Nov 18 '20 15:11 ArjobanSingh

@ArjobanSingh If possible, please try and upgrade your project to it and report any issues you encounter. I have tested it on all the examples from the site and it works as expected, with certain modifications related to the deprecations.

petyosi avatar Nov 18 '20 16:11 petyosi

:tada: This issue has been resolved in version 1.2.2 :tada:

The release is available on:

Your semantic-release bot :package::rocket:

github-actions[bot] avatar Dec 30 '20 07:12 github-actions[bot]

Hopefully, the introduced change addresses the problem - refreshing the data resets the endReached state, even if the data has the same length as the previous one.

petyosi avatar Dec 30 '20 07:12 petyosi

@petyosi I feel like sometimes endReached is not called and I'm not sure why... we're using apollo too, using data instead of totalCount seems to help, but the issue still there... is there anything I can do to prevent it?

felippepuhle avatar Jan 22 '21 20:01 felippepuhle

@felippepuhle Nothing I can recommend without a repo. If you get one, please open a new issue. Thank you!

petyosi avatar Jan 22 '21 20:01 petyosi

@petyosi not sure if I should open a new issue as I don't know exactly how to create/reproduce it(it's not constant but it happens a lot sometimes)... I was digging for a bit and this condition seems to be causing it... for example, after an initial load items[items.length - 1].originalIndex is 9 but totalCount - 1 is -1...

atBottomStateChange works 100% of the time but it's not exactly what we want... is there anything else I can do so we can better investigate and fix this?

thanks!

felippepuhle avatar Feb 02 '21 20:02 felippepuhle

@felippepuhle I can't say for sure what happens. You can try throwing a throttleTime (like startReached here) before the filter and see if it makes any difference.

petyosi avatar Feb 03 '21 11:02 petyosi

It does seem to help @petyosi, can we add it and release a new version(I can open a PR if you want)? thanks!

felippepuhle avatar Feb 03 '21 14:02 felippepuhle

Reopening this as a reminder. I will look into it shortly.

petyosi avatar Feb 03 '21 14:02 petyosi

@petyosi Not sure this object reference change fix extends to grouped virtuoso since data prop isn't passed in when using it. Will changing the groupCounts also reset the endReached? Also, do you reset startReached too?

muyiwaoyeniyi avatar Feb 10 '21 16:02 muyiwaoyeniyi

@muyiwaoyeniyi no, the scope of this discussion does not include grouped mode. It would help me if you can report your problem in a separate issue, including reproduction and some context on what you are trying to achieve (and why it does not work).

petyosi avatar Feb 10 '21 17:02 petyosi

@petyosi Thanks for quick response and awesome package. I will open another issue on grouped mode.

For resetting endReached, do you also reset startReached?

muyiwaoyeniyi avatar Feb 10 '21 17:02 muyiwaoyeniyi

For resetting endReached, do you also reset startReached?

No - I am not sure why that would be necessary. Please provide an example + context - thanks!

petyosi avatar Feb 10 '21 17:02 petyosi

The context is similar to what was described in this comment https://github.com/petyosi/react-virtuoso/issues/182#issuecomment-729567934 .. I have a slack-like scrolling container that let's a user scroll and fetch data in both directions. For any number of reasons, it's possible to want the startReached (like the endReached) to be re-triggered.

muyiwaoyeniyi avatar Feb 10 '21 17:02 muyiwaoyeniyi

@petyosi endReached issue seems to be unresolved... the solution suggested by you earlier for throttle time, can we create a fix based on that?

heypran avatar Nov 17 '21 11:11 heypran