How to make Optimistic Updates in useInfiniteQuery #3360
Replies: 6 comments 10 replies
-
I have the same question. I've read the docs and optimistic updates with Sorry, this isn't an answer but I wanted to +1 this and add some supplementary detail to the question. But here's what I did (assume
|
Beta Was this translation helpful? Give feedback.
-
What should I do if I want to implement optimistic delete? For example:
My hack is to replace the deleted item with |
Beta Was this translation helpful? Give feedback.
-
is there any way to queryClient.invalidateQueries(["fetchPages"]); |
Beta Was this translation helpful? Give feedback.
-
All actions performed on infinite data are a bit cumbersome and should probably be abstracted with some common defaults, even as an opt in or helpers. It could also be nice to add an option to have the data flattened in cache, I think this will serve a lot of common use cases. |
Beta Was this translation helpful? Give feedback.
-
i would flatten the data before mapping and then just use lodash's chunk to chunk them back to each page size... assuming each page has the same size e.g a paginated endpoint. as for const newData = oldData.pages.flat().map((comments) => {
// do your thing
});
const newPages = chunk(newData, 3)
return {
pageParams: [undefined, ...newPages.slice(0, -1).map((page) => page.at(-1).createdAt)]
pages: newPages,
} On this line: [undefined, ...newPages.slice(0, -1).map((page => page.at(-1).createdAt)]
To visualize: const alphabets = [
[A, B, C],
[D, E, F],
[G, H, I],
]
const pages = alphabets
const pageParams = [undefined, C, F],
// Fetch requests would be
fetch('/alphabets?limit=3')
fetch('/alphabets?limit=3&after=C')
fetch('/alphabets?limit=3&after=F') |
Beta Was this translation helpful? Give feedback.
-
Better with immer
|
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
How to make Optimistic Updates in useInfiniteQuery
Beta Was this translation helpful? Give feedback.
All reactions