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
mutatefunction with theundefinedvalue to invalidate the mutation cache. So, in youronSuccesscallback, you can try callingmutate(undefined)to invalidate theuseUserSearchhook mutation.another way of doing this is to use
queryClient.invalidateQueries()method with theexactflag set tofalseto invalidate all mutations with the same key. So, in youronSuccesscallback, you can try callingqueryClient.invalidateQueries("user-search", {exact: false})to invalidate theuseUserSearchhook mutation.