You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
We added better Optimistic UI support in v1.2.0. However, what if your API is only returning a subset of the data (such as the mutated part), that can be populated into the cache? Usually, an extra revalidation after that mutation is needed. But now you can also use a function as populateCache to transform the mutate result into the full data:
awaitmutate(addTodo(newTodo),{optimisticData: [...data,newTodo],rollbackOnError: true,populateCache: (addedTodo,currentData)=>{// `addedTodo` is what the API returns. It's not// returning a list of all current todos but only// the new added one.// In this case, we can transform the mutate result// together with current data, into the new data// that can be updated.return[...currentData,addedTodo];},// Since the API already gives us the updated information,// we don't need to revalidate here.revalidate: false,});
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
Highlights of This Release
populateCache
Option Now Supports FunctionWe added better Optimistic UI support in v1.2.0. However, what if your API is only returning a subset of the data (such as the mutated part), that can be populated into the cache? Usually, an extra revalidation after that mutation is needed. But now you can also use a function as
populateCache
to transform the mutate result into the full data:Here is a demo for it: https://codesandbox.io/s/swr-basic-forked-hi9svh
Bug Fixes
What's Changed
populateCache
as a function by @shuding in feat: SupportpopulateCache
as a function #1818Full Changelog: 1.2.1...1.2.2
This discussion was created from the release 1.2.2.
Beta Was this translation helpful? Give feedback.
All reactions