use-pagination-firestore icon indicating copy to clipboard operation
use-pagination-firestore copied to clipboard

limitToLast() null when limit is set to 1 at first page

Open MorenoMdz opened this issue 4 years ago • 5 comments
trafficstars

While I am doing some local tests, setting the limit to 1 to test with a small dataset, some use cases might want to paginate items one by one, right now it is breaking in when you navigate forward and return to the first page as it still shows the "previous" button.

Might be fixable by saving the first found item Ref and comparing it to the first in the current list, if equal set isStart to true.

image

MorenoMdz avatar Apr 05 '21 19:04 MorenoMdz

Setting the limit to one breaks it: https://user-images.githubusercontent.com/29472480/113618773-83dea280-962e-11eb-86d0-57afcff7e025.mov

While setting it to 2+ works fine: https://user-images.githubusercontent.com/29472480/113618736-79240d80-962e-11eb-8efa-b33294a4cd5d.mov

Easier to see with videos!

MorenoMdz avatar Apr 05 '21 19:04 MorenoMdz

hey @MorenoMdz I'm not sure this is related to the hook – looks like you need to provide an orderBy clause when you use limitToLast: https://googleapis.dev/nodejs/firestore/latest/Query.html#limitToLast

premshree avatar Apr 05 '21 20:04 premshree

hey @MorenoMdz I'm not sure this is related to the hook – looks like you need to provide an orderBy clause when you use limitToLast: https://googleapis.dev/nodejs/firestore/latest/Query.html#limitToLast

I think it is related, updating to this issue there is something weird in the flow:

https://user-images.githubusercontent.com/29472480/113620856-416a9500-9631-11eb-8ac1-9fa918e40474.mov

I have added the hook in most my screens with list without issues, but this one for some reason is reading the list head and tail wrong, in this case if I don't have an orderBy set it will crash, but as you see in the video the list is not acting correctly.

  const { items, isLoading, isStart, isEnd, getPrev, getNext } = usePagination(
    db
      .collection('users')
      .where('organizationId', '==', profile.organizationId)
      .orderBy('name', 'asc'),
    {
      limit: 10
    }
  );

Edit: Setting the orderBy() made it work with the limit set to 1. Should I keep the "users" list issue here or close this one and open another one?

MorenoMdz avatar Apr 05 '21 20:04 MorenoMdz

Oh! I believe I understand the issue @MorenoMdz – I'll handle this separately in a while. Sorry about that!

Edit: just to clarify, the issue I'm seeing is that if a query doesn't pass an orderBy clause, going back does indeed crash. I don't know off the top what the solution would like, but I'll try to dig in. Thanks for pointing it out!

premshree avatar Apr 05 '21 20:04 premshree

Oh! I believe I understand the issue @MorenoMdz – I'll handle this separately in a while. Sorry about that!

Edit: just to clarify, the issue I'm seeing is that if a query doesn't pass an orderBy clause, going back does indeed crash. I don't know off the top what the solution would like, but I'll try to dig in. Thanks for pointing it out!

Sounds good. Do you think that is what's causing the issue of the weird paging in the second comment?

image

During the mounting, this is how the variables are being set, for some reason that specific query is causing the hook to update the start variable to false (sorry missed the last line in the log there, but it goes to isStart = false).

MorenoMdz avatar Apr 05 '21 21:04 MorenoMdz