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
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!
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
useSWRPagescalleddepswhich 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 thatdepsis of type array, so you should pass something like["chat:channel:<chanel-id>"].Also, when using
depsI've noticed that the "key" passed touseSWRPagesshould be static. If not, weird stuff happens such as two fetch data being trigged when thedepschanges.