DEV Community

Discussion on: Using Paginated Data with SWR

Collapse
stunaz profile image
stunaz • Edited on

Hi, curious to understand why useSWRPages event exists? couldn't we simply use useSWR and pass different key (full url with pagination request param i.e. &offset=... or ?page=?&limit=) ... is there a real benefit (perf, caching...) for using useSWRPages or it 's only a nice shortcut?

Collapse
sergiodxa profile image
Sergio Daniel Xalambrí Author

If you use the same useSWR with different keys you are going to replace the current page with the new one, that works great if you only show a single page at the time, useSWRPages is used to implement paginated infinite scroll.

It also comes with the nice feature of keeping the amount of pages already loaded in cache, so the next time the user visits a page it will render the same amount of times instead of only the first page, this works great when going back to the previous page and helps keep the scroll in the same position the user was before navigating.

Collapse
stunaz profile image
stunaz

Nice! thank you very much for that