-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support populateCache
as a function
#1817
Comments
Hi @shuding, thank you for this improvement! I have a similar need with the library. I would like to use Could you do that ? Thanks! |
@ludovic-montel For this do you mean the current returned Or are you using the global |
I think then the mutate(addTodo(todo), {
optimisticData: todos => [...todos, todo],
populateCache: (addedTodo, todos) => [...todos, addedTodo],
rollbackOnError: true,
revalidate: true,
}) So this can be wrapped in an event callback which is totally pure and doesn't depend on the hook state. |
Yeah building up hook with |
@huozhi yes, exactly, it would allow to use the global mutate function with the |
This can be a great separate feature request 👍 |
Thanks for your answers, I just added a feature request in the Discussion of the repository (#1850) |
We added the
populateCache
option (as well as other options for optimistic UI) recently, a normal use case looks like the following:Say that the
addTodo
calls thePOST /api/todos
endpoint, which returns the full, updated list.Transform Before Populating the Cache
However, it's also common that the
POST /api/todos
endpoint only returns the new added item, not the full list. So naturally the next step is to supportpopulateCache
as a synchronous function that transforms the returned data before writing back to the cache:Note that
revalidate: true
is needed now.Also
populateCache
is synchronous only to avoid possible race conditions.Opt-out
With this design, if an error is throw when calling
populateCache
, we can skip populating the cache for this specific mutation.This enables a way to conditionally populating the cache.
The text was updated successfully, but these errors were encountered: