question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Pagination example with page numbers?

See original GitHub issue

Our backend uses page number for pagination (first page is 0, second page is 1, and so on). What would be the correct way to implement that with useSWRPages()?

My current implementation works so that I’m using this as my swrDataToOffset function:

data => !data || !pages || pages.length === 0 ? 0 : pages.length - 1

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.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
roxeteercommented, Oct 30, 2019

I think got rid of the exceptions by changing the swrDataToOffset function to return pages.length instead of pages.length - 1. But now after loading more data after SWR displays the cached data, I get the last page again. I’ll try to create a simplified version of our component for an example.

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 null from the page component doesn’t seem to have an effect.

1reaction
shudingcommented, Nov 7, 2019

Hi @grazianodev !

I noticed the exact same bug as you had, and already created a PR to fix it (there’s also an example): https://github.com/zeit/swr/pull/95

We will review and ship it today in the next version, will let you know!

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to handle pagination with page numbers? - Octoparse
Go to have a check now! “Next” button for pagination is not always available and pagination on some websites may be done by...
Read more >
APA 7th Edition Style Guide: Pagination - IRSC Libraries
If the pagination is continuous, separate pages by a hyphen. · If the pagination is discontinuous, separate groupings of page numbers by a...
Read more >
Pagination – Examples And Good Practices
Pagination With Manual Page Input #. In some cases users can provide the number of the page they'd like to see manually, via...
Read more >
Limit pagination page number - php - Stack Overflow
This is the code I use to create pagination page for data search.But I just noticed that I have too many records then...
Read more >
Pagination & Page Breaks in MS Word (Quick Visual Guide)
If your only reason for pagination is to keep track of the number of pages your document has and which page you're on,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found