TanStackT
TanStack6mo ago
9 replies
full-green

Invalidating queryKey with variable doesn't work

Hi guys,

I'm encountering issues when trying to invalidate queries with a variable in the query key during the onSettled of a useMutation.
Basically trying something like this (with version 5.84.1):

export function usePatchSomething(): UseMutationResult<Something, ErrorType, Something> {
    const queryClient = useQueryClient();

    return useMutation<Something, ErrorType, Something>({
        mutationFn: async (data: Something) => mutateSomething.update.fn(data),
        onSettled: async (_, __, variables) => {
            const { id } = variables;
            void queryClient.invalidateQueries({ queryKey: [SOMETHING, id] });
        },
    });
}


Something has a prop id, when logging the variables in onSettled I do get a result.
When I hardcode the id into the key directly, it works fine, but not when using the variable. Is this a bug or is that simply not possible?
Is there some other way I can achieve the same effect?
Was this page helpful?