Replies: 1 comment
-
Overall it's becase useSWRInfinite in the end use two caches, one global based on fist page key, and then each page has it’s own, initial run paged one is used. @0xc14m1z imho current workaround would be to update both, something like https://codesandbox.io/s/swr-infinite-mutate-forked-7hd4nw?file=/src/App.tsx |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Bug report
Description / Observed Behavior
We are using
useSWRInfinite
to paginate data by a cursor that we receive from the backend to build kind of a social feed.When a user posts, we would like to perform an optimistic update such that the new element is prepended to the first page.
To achieve that, we use the bound
mutate
we get fromuseSWRInfinite
to prepend the new element in the first page, and update the cursor of the last page, pushing the next selection forward by 1, to skip the new element.After prepending the new element, the feed, the internal representation and the cache contains correct data.
When fetching additional data, though, it still uses stale data when calling the fetcher.
Expected Behavior
I'd expect that, when generating the cache key for a new page fetch, the most updated element from the cache is available as
previousPageData
.Repro Steps / Code Example
Here is a CodeSandbox with a bit more explained: https://codesandbox.io/s/swr-infinite-mutate-89swnb?file=/src/App.tsx
Additional Context
Tried with both
swr
version 2.0.3 and 2.0.4Beta Was this translation helpful? Give feedback.
All reactions