Skip to content

Instantly share code, notes, and snippets.

@srph
Last active July 22, 2026 18:37
Show Gist options
  • Select an option

  • Save srph/75b3c05a0129b04059f14a999b3ee45c to your computer and use it in GitHub Desktop.

Select an option

Save srph/75b3c05a0129b04059f14a999b3ee45c to your computer and use it in GitHub Desktop.
React: useLatestMutationState - An ergonomic wrapper for react-query's useMutationState

what

An ergonomic wrapper for react-query's useMutationState

usage

export function useCreatePostMutation(...) {
  return useMutation({ mutationKey: ['create-post'], ... })
}

export function useCreatePostMutationState(...) {
  return useLatestMutationState({ mutationKey: ['create-post'] })
}
import { useMutationState } from "@tanstack/react-query";
export function useLatestMutationState(mutationKey: unknown[]) {
const mutation = useMutationState({
filters: { mutationKey },
select: (entry) => entry.state,
});
const latest = mutation.at(-1);
return {
isPending: latest?.status === "pending",
isError: latest?.status === "error",
isSuccess: latest?.status === "success",
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment