mirror of
https://github.com/TanStack/query.git
synced 2026-11-16 22:50:20 +00:00
* docs(vue-query,solid-query): inline react reference content instead of 'ref'-ing hand-written react docs * docs(react-query): generate reference docs with TypeDoc * docs(vue-query,solid-query): fix dangling suspense-hook links left over from the react content copy * docs(solid-query): apply solid's callback call form to inlined reference examples * docs(react-query): correct inaccurate JSDoc statements flagged in review * docs(vue-query,solid-query): fix reactivity-aware examples and inaccurate type signatures flagged in review * docs(vue-query,solid-query): fix remaining type/behavior inaccuracies flagged in second review * docs(react-query): stop implying a default queryFn defers a fetch the way 'enabled: false' does * docs(vue-query): mark 'mutate'/'mutateAsync' per-call options as optional
2.4 KiB
2.4 KiB
| id | title |
|---|---|
| useMutationState | useMutationState |
useMutationState is a hook that gives you access to all mutations in the MutationCache. You can pass filters to it to narrow down your mutations, and select to transform the mutation state.
Example 1: Get all variables of all running mutations
import { useMutationState } from '@tanstack/solid-query'
const variables = useMutationState(() => ({
filters: { status: 'pending' },
select: (mutation) => mutation.state.variables,
}))
// Access the current variables
variables()
Example 2: Get all data for specific mutations via the mutationKey
import { useMutation, useMutationState } from '@tanstack/solid-query'
const mutationKey = ['posts']
// Some mutation that we want to get the state for
const mutation = useMutation(() => ({
mutationKey,
mutationFn: (newPost) => {
return axios.post('/posts', newPost)
},
}))
const data = useMutationState(() => ({
// this mutation key needs to match the mutation key of the given mutation (see above)
filters: { mutationKey },
select: (mutation) => mutation.state.data,
}))
Example 3: Access the latest mutation data via the mutationKey.
Each invocation of mutate adds a new entry to the mutation cache for gcTime milliseconds.
To access the latest invocation, you can check for the last item that useMutationState returns.
import { useMutation, useMutationState } from '@tanstack/solid-query'
const mutationKey = ['posts']
// Some mutation that we want to get the state for
const mutation = useMutation(() => ({
mutationKey,
mutationFn: (newPost) => {
return axios.post('/posts', newPost)
},
}))
const data = useMutationState(() => ({
// this mutation key needs to match the mutation key of the given mutation (see above)
filters: { mutationKey },
select: (mutation) => mutation.state.data,
}))
// Latest mutation data
const latest = data()[data().length - 1]
Options
optionsfilters?: MutationFilters: Mutation Filtersselect?: (mutation: Mutation) => TResult- Use this to transform the mutation state.
queryClient?: Accessor<QueryClient>- Use this to use a custom QueryClient. Otherwise, the one from the nearest context will be used.
Returns
Accessor<Array<TResult>>- Will resolve to an Array of whatever
selectreturns for each matching mutation.
- Will resolve to an Array of whatever