SWR 2.0 coming soon, and this is the first beta version!
Keep in mind that APIs might still change until the stable release. Documentation will also be updated once stable.
💖 Give feedback in discussion: https://github.com/vercel/swr/discussions/1919.
Added in #1450, the new useSWRMutation hook covers all the use cases of:
isValidating but for mutations.Here's a quick example of how it looks:
import useSWRMutation from 'swr/mutation'
async function sendRequest(url, { arg }) {
return fetch(url, {
method: 'POST',
body: JSON.stringify(arg)
})
}
function App() {
const { trigger } = useSWRMutation('/api/user', sendRequest)
return <button onClick={() => {
trigger({ username: 'johndoe' })
}}>Create User</button>
}
In this example, the "fetcher", which is sendRequest, will receive the value { username: 'johndoe' } as the arg from the second parameter. The request will only be triggered when clicking the button.
The new useSWRMutation hook is actually more powerful than this, it also supports:
useSWRuseSWR after mutation finishesMore examples to come.
Previously, if the key is an array, the values will be passed to the fetcher function as arguments separately. In 2.0, the key will always be passed to the fetcher as is.
Before:
// SWR 1.x
useSWR([1, 2, 3], (a, b, c) => {
assert(a === 1)
assert(b === 2)
assert(c === 3)
})
After 2.0.0:
// SWR 2.0.0
useSWR([1, 2, 3], (a) => {
assert(a === [1, 2, 3])
})
This change affects the code that directly reads/writes to the cache, or provides a cache preset. For example if you have something like cache.set(key, value), you'll have to update your code.
Previously, the cached value of key will be the associated data, so this was guaranteed:
// SWR 1.x
assert(cache.get(key) === data)
And we keep other states (error, isValidating) with a special, prefixed key. Something like '$err$' + key.
Since 2.0.0, the internal structure will be an object that holds all the current states:
// SWR 2.0.0
assert(cache.get(key) === { data, error, isValidating })
So you will have to do the following change to your code, get:
- cache.get(key)
+ cache.get(key)?.data
And set:
- cache.set(key, data)
+ cache.set(key, { ...cache.get(key), data })
Full Changelog: https://github.com/vercel/swr/compare/1.2.2...2.0.0-beta.0
Fetched April 18, 2026