Capítulo 68 de 80
Gives read access to every mutation in the MutationCache, filtered and transformed via select — the mechanism behind cross-component optimistic-UI (Chapter 30's "via the UI, different component" pattern) and behind reading the latest invocation of a repeatedly-called mutation.
useMutationState({ filters, select }, queryClient?) — filters is a MutationFilters object (commonly { mutationKey, status: 'pending' }); select(mutation) transforms each matching Mutation instance into whatever shape the component needs (e.g. mutation.state.variables).mutationKey, multiple concurrent invocations can coexist — the result reflects all of them, in invocation order.mutate() call adds a new entry to the cache, kept for gcTime; to read only the most recent one, take the last element of the returned array (data[data.length - 1]).mutationKey on the triggering useMutation with useMutationState({ filters: { mutationKey, status: 'pending' }, select: (m) => m.state.variables }) elsewhere in the tree lets any component read a pending mutation's variables without prop drilling or a shared cache write.// Anywhere in the tree — read variables of all currently-pending 'addTodo' mutations
const variables = useMutationState({
filters: { mutationKey: ['addTodo'], status: 'pending' },
select: (mutation) => mutation.state.variables,
})
mutationKey.mutationKey with a matching filter here.mutationKey, the shared identifier this hook filters on.