Highlights of This Release
Dedicated API for Optimistic Updates with Auto Rollback on Error
There are now some new options in mutate
:
mutate(patchUser(user), {
optimisticData: user,
populateCache: true,
rollbackOnError: true,
revalidate: true,
})
Here the cache will be immediately updated to user
, the “optimistic value”. And then a request (remote mutation) is started via patchUser(user)
and the response will be written to the cache. If that request fails, the original result will be rolled back safely so the optimistic value will be gone. And after all those finish, a revalidation will start to fetch the latest value.
This is extremely helpful for building the optimistic UI pattern.
You can do the same for the global mutate
, just remember to pass the key. Also, the current mutate
APIs stay unchanged so mutate(data, false)
works the same.
Here's an example: https://codesandbox.io/s/swr-basic-forked-k5hps.
What's Changed
- feat: Optimistic mutation with error rollback by @shuding in #1745
- chore: Ignore coverage report by @shuding in #1744
- chore: Merge mutation states by @shuding in #1748
- chore: Rename CONCURRENT_REQUESTS to FETCH by @shuding in #1749
- Merge extra request states in cache by @shuding in #1752
Full Changelog: 1.1.2...1.2.0-beta.0