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/react-query'
const variables = useMutationState({ filters: { status: 'pending' }, select: (mutation) => mutation.state.variables,})
Example 2: Get all data for specific mutations via the mutationKey
import { useMutation, useMutationState } from '@tanstack/react-query'
const mutationKey = ['posts']
// Some mutation that we want to get the state forconst 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/react-query'
const mutationKey = ['posts']
// Some mutation that we want to get the state forconst 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 dataconst latest = data[data.length - 1]
Options
options
filters?: MutationFilters
: Mutation Filtersselect?: (mutation: Mutation) => TResult
queryClient?: QueryClient
,
Returns
Array<TResult>
select
returns for each matching mutation.“This course is the best way to learn how to use React Query in real-world applications.”—Tanner LinsleyCheck it out