框架
版本

MutationCache

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。)

tsx
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 (它可用的方法有)

选项

  • onError?: (error: unknown, variables: unknown, context: unknown, mutation: Mutation) => Promise<unknown> | unknown
    • Optional (可选)
    • This function will be called if some mutation encounters an error. (如果某个 mutation 遇到错误,将调用此函数。)
    • If you return a Promise from it, it will be awaited (如果您从此函数返回一个 Promise,它将被等待执行)
  • onSuccess?: (data: unknown, variables: unknown, context: unknown, mutation: Mutation) => Promise<unknown> | unknown
    • Optional (可选)
    • This function will be called if some mutation is successful. (如果某个 mutation 成功,将调用此函数。)
    • If you return a Promise from it, it will be awaited (如果您从此函数返回一个 Promise,它将被等待执行)
  • onSettled?: (data: unknown | undefined, error: unknown | null, variables: unknown, context: unknown, mutation: Mutation) => Promise<unknown> | unknown
    • Optional (可选)
    • This function will be called if some mutation is settled (either successful or errored). (如果某个 mutation 已经完成(无论成功还是失败),将调用此函数。)
    • If you return a Promise from it, it will be awaited (如果您从此函数返回一个 Promise,它将被等待执行)
  • onMutate?: (variables: unknown, mutation: Mutation) => Promise<unknown> | unknown
    • Optional (可选)
    • This function will be called before some mutation executes. (在某个 mutation 执行之前调用此函数。)
    • If you return a Promise from it, it will be awaited (如果您从此函数返回一个 Promise,它将被等待执行)

全局回调

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 不同,因为)

  • defaultOptions can be overridden by each Mutation - the global callbacks will always be called. (defaultOptions 可被每个 Mutation 覆盖 - 全局回调将始终被调用。)
  • onMutate does not allow returning a context value. (onMutate 不允许返回 context 值。)

mutationCache.getAll

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 的信息时会很有用)

tsx
const mutations = mutationCache.getAll()
const mutations = mutationCache.getAll()

Returns (返回)

  • Mutation[]
    • Mutation instances from the cache (缓存中的 Mutation 实例)

mutationCache.subscribe

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 被更新、添加或移除。)

tsx
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)

选项

  • callback: (mutation?: MutationCacheNotifyEvent) => void
    • This function will be called with the mutation cache any time it is updated. (每次 mutation cache 更新时,都会调用此函数并传入 mutation cache。)

Returns (返回)

  • unsubscribe: Function => void
    • This function will unsubscribe the callback from the mutation cache. (此函数将取消 callback 对 mutation cache 的订阅。)

mutationCache.clear

The clear method can be used to clear the cache entirely and start fresh. (clear 方法可用于完全清除缓存并重新开始。)

tsx
mutationCache.clear()
mutationCache.clear()