TanStackT
TanStack11mo ago
15 replies
dry-scarlet

Is there a way to reset mutations based on key?

My apologies - this is a duplicate of react-query-questionsCan I reset all mutation state similar to how I can clear all queries based on key? but that didn't have an answer. I am pretty sure I have a legit use case for this: I have a hook that keeps track of pending & successful mutations:

export function useLeftConvos() {
  const pending = useMutationState({
    filters: {mutationKey: [RQKEY_ROOT], status: 'pending'},
    select: mutation => mutation.options.mutationKey?.[1] as string | undefined,
  })
  const success = useMutationState({
    filters: {mutationKey: [RQKEY_ROOT], status: 'success'},
    select: mutation => mutation.options.mutationKey?.[1] as string | undefined,
  })
  return useMemo(
    () => [...pending, ...success].filter(id => id !== undefined),
    [pending, success],
  )
}


I can then use this hook to filter out items from a list elsewhere.

Sometimes, I want to remove an ID from this list. Is there a way to reset a mutation, like how you can do queryClient.resetQueries({ queryKey: your_key_here })? I can't use the reset function returned by the hook, because I don't know what the ID is in advance.

If you're interested, the mutation is here - it's for leaving a DM conversation: https://github.com/bluesky-social/social-app/blob/main/src/state/queries/messages/leave-conversation.ts
and I want to reset it when a new conversation begins, here: https://github.com/bluesky-social/social-app/blob/main/src/state/queries/messages/list-conversations.tsx#L105

Many thanks!
Was this page helpful?