Hi I have below mutation :
export const useUserSearch = (data) => {
return useMutation({
mutationKey: ["user-search", data],
mutationFn: fetchUser,
});
};
I am using it in the component :
const {
mutate,
data: userInfo,
isLoading: userInfoLoading,
} = useUserSearch();
Now I have another Mutation which gets called on updation request like below :
const handleCreateRoleConfirm = (data) => {
createRoleMutate(data, {
onSuccess: () => {
queryClient.invalidateQueries("user-search"); // trying to invalidate the above mutaiton
showAlert({
message: "The record has been created successfully",
variant: "success",
});
setIsAddDialogOpen(false);
},
});
};
Now here in the onsuccess I am trying to invalidate the useUserSearch hooks mutation . But it does not work. Seems like it is not invalidation it an rerigging the api . How do I fix this ?
you can call the
mutate
function with theundefined
value to invalidate the mutation cache. So, in youronSuccess
callback, you can try callingmutate(undefined)
to invalidate theuseUserSearch
hook mutation.another way of doing this is to use
queryClient.invalidateQueries()
method with theexact
flag set tofalse
to invalidate all mutations with the same key. So, in youronSuccess
callback, you can try callingqueryClient.invalidateQueries("user-search", {exact: false})
to invalidate theuseUserSearch
hook mutation.