Swr: `useSWRPages` does not reset its state when pageKey changes

Created on 15 Nov 2019  路  3Comments  路  Source: vercel/swr

Hi again!

I have a component that uses useSWRPages to display a paginated chat.
useSWRPages has a key that looks like chat:channel:<channel-id>

the issue is that when the channel ID changes (and therefore the key changes), useSWRPages does not reset its internal state, so it still uses the pageCount, pageOffsets, and pageSWRs of the previous channel (resulting in strange behaviors such as displaying duplicated content or content from another channel).

I have a workaround where I basically put a key on my component to force reset it completely, but this sounds like a bug

I think the solution is simple, you need a setState that is able to reset its state to the default value whenever its dependencies change

bug pagination

Most helpful comment

I was trying to come up with a fix in SWR's code when I realised that there's a 4th argument in the useSWRPages called deps which one is supposed to use to make pages reset according to a value. I've tried it out and it is working as expected. Note that deps is of type array, so you should pass something like ["chat:channel:<chanel-id>"].

Also, when using deps I've noticed that the "key" passed to useSWRPages should be static. If not, weird stuff happens such as two fetch data being trigged when the deps changes.

All 3 comments

I'm also having this issue and would really appreciate if it got fixed. For the time being I'm considering calling trigger('page-key') from the button that should cause the list to be refreshed. This is a very hacky and temporal solution though...

I was trying to come up with a fix in SWR's code when I realised that there's a 4th argument in the useSWRPages called deps which one is supposed to use to make pages reset according to a value. I've tried it out and it is working as expected. Note that deps is of type array, so you should pass something like ["chat:channel:<chanel-id>"].

Also, when using deps I've noticed that the "key" passed to useSWRPages should be static. If not, weird stuff happens such as two fetch data being trigged when the deps changes.

The new pagination API has launched: https://swr.vercel.app/docs/pagination. Feel free to reopen if you are still encountering an issue!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

bbenezech picture bbenezech  路  5Comments

baoduy picture baoduy  路  4Comments

timurmaio picture timurmaio  路  3Comments

bywo picture bywo  路  4Comments

tiagocorreiaalmeida picture tiagocorreiaalmeida  路  3Comments