The MutationCache is the storage for mutations. (MutationCache 是 mutations 的存储库。)
Normally, you will not interact with the MutationCache directly and instead use the QueryClient. (通常情况下,您不会直接与 MutationCache 交互,而是使用 QueryClient。)
import { MutationCache } from '@tanstack/react-query'
const mutationCache = new MutationCache({
onError: (error) => {
console.log(error)
},
onSuccess: (data) => {
console.log(data)
},
})
import { MutationCache } from '@tanstack/react-query'
const mutationCache = new MutationCache({
onError: (error) => {
console.log(error)
},
onSuccess: (data) => {
console.log(data)
},
})
Its available methods are (它可用的方法有)
选项
The onError, onSuccess, onSettled and onMutate callbacks on the MutationCache can be used to handle these events on a global level. They are different to defaultOptions provided to the QueryClient because (MutationCache 上的 onError、onSuccess、onSettled 和 onMutate 回调可用于全局处理这些事件。它们与提供给 QueryClient 的 defaultOptions 不同,因为)
getAll returns all mutations within the cache. (getAll 返回缓存中的所有 mutations。)
Note: This is not typically needed for most applications, but can come in handy when needing more information about a mutation in rare scenarios (注意:这通常不需要用于大多数应用程序,但在少数情况下需要更多关于 mutation 的信息时会很有用)
const mutations = mutationCache.getAll()
const mutations = mutationCache.getAll()
Returns (返回)
The subscribe method can be used to subscribe to the mutation cache as a whole and be informed of safe/known updates to the cache like mutation states changing or mutations being updated, added or removed. (subscribe 方法可用于订阅整个 mutation cache,并获知缓存中的安全/已知更新,例如 mutation 状态更改或 mutations 被更新、添加或移除。)
const callback = (event) => {
console.log(event.type, event.mutation)
}
const unsubscribe = mutationCache.subscribe(callback)
const callback = (event) => {
console.log(event.type, event.mutation)
}
const unsubscribe = mutationCache.subscribe(callback)
选项
Returns (返回)
The clear method can be used to clear the cache entirely and start fresh. (clear 方法可用于完全清除缓存并重新开始。)
mutationCache.clear()
mutationCache.clear()