-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Pagination example with page numbers? #44
Comments
I think got rid of the exceptions by changing the One more thing: is there a way for a page to tell that there will be no more data available (so I could hide the element triggering a fetch)? Now I get more and more pages even if they are empty. Returning |
Hi @roxeteer ! I think we'll need a second parameter like (data, pageSWRs) => pageSWRs.length as your Regarding
There's a const { isReachingEnd } = useSWRPages(
...,
...,
(data, pageSWRs) => {
if (!data.hasNextPage) return null
return pageSWRs.length
}
) You can conditionally return |
👋 this feature has landed in https://codesandbox.io/s/swr-playground-946db Closing this issue now. |
Great! Never had the chance to return to my SWR experiments. I'll see what I can achieve with v0.1.7 when I have my other project tasks done. |
Hi, thank you for your efforts, however it seems like something's not working well, even in the example you posted on CodeSandbox. If I click the 'load next page' button a few times you get the following, which is correct:
However if I switch to another tab or window and return to CodeSandbox, this is what I see:
I've implemented the example working with a real API and I see the error again: I get the data just fine initially, but if I switch tab or window and go back, suddenly the original data is replaced with repeated content from the same page. Basically, it behaves as described above. Restoring the scrolling position doesn't seem to work either: if from page A (where I fetch my data) I scroll 400px, then I visit page B and scroll e.g. 200px, and then go back to page A via the back button, I would expect to be at 400px from the top but I'm actually at 200px (in other words, it applies to page A the last scrolling position of page B). I really like the potential of this library so it would be great if I could get this working! Please note that I'm not a very experienced developer so I might be doing something wrong or missing something. Thank you for any help! |
Hi @grazianodev ! I noticed the exact same bug as you had, and already created a PR to fix it (there's also an example): #95 We will review and ship it today in the next version, will let you know! |
We just shipped the improvements in |
Thank you for your work! I have made some tests with the new version and the bug I described earlier is definitely gone. However I still have some issues with restoring the scrolling position. I am currently testing with a frontend that makes an API request to fetch a list of posts from a backend that uses a Wordpress installation. When I visit the page of a single post from the homepage (where the list of posts is), then hit the back button to return to the homepage, the SWR library requests the lists of posts again (to revalidate the data, I imagine), which takes some time; but the scrolling position is restored immediately, so what happens is that the restoration is done when I'm still on the single page, causing a jump in this page before returning to the homepage, which is a bit nasty. Another issue I've noticed with scrolling is that if I've scrolled 3000px on the homepage, and visit a single post page that is 2000px tall, then hit the back button, the restoration of the scrolling position is not precise; on the contrary, if the single page is taller than what I've initially scrolled it is always correct. (Just pointing it out, I'm not even sure if this being super precise is within the scope of SWR). I do have one more question. In this use case (just retrieving a set of posts) I don't really need to revalidate the data, so is there a way to just fetch the cached posts without revalidating when I return to the homepage? (I guess this would conveniently also solve the first scrolling issue since, with no revalidation requests, hitting the back button would immediately bring me back to the homepage). |
@grazianodev is this a client side transition (e.g.:
If not, SWR cannot reuse the cache (since it's in the memory) and restore the list unfortunately.
Yeah we plan to provide that option 👍 |
Our backend uses page number for pagination (first page is
0
, second page is1
, and so on). What would be the correct way to implement that withuseSWRPages()
?My current implementation works so that I'm using this as my
swrDataToOffset
function:Also, I'm not sure if it's a problem with my implementation, but as I'm trying
useSWRPages()
out in our Next.js application, I'm getting this exception when the application is hot reloaded (HMR) and there were several pages fetched before the reload:Warning: Encountered two children with the same key, `page-2`. Keys should be unique so that components maintain their identity across updates. Non-unique keys may cause children to be duplicated and/or omitted — the behavior is unsupported and could change in a future version.
The text was updated successfully, but these errors were encountered: