T
TanStack2y ago
exotic-emerald

Refetch mutation after user action.

Hi ! I have a useMutation to get datas and display it on a Table. I would like to refetch my datas without havin to refresh de page. but i don't understand how can i use my queryClient to trigger the refetch.
const queryClient = useQueryClient()
const offersMutation = useMutation(
() => getOffers(),
{
onSuccess: (data => {
console.log("Fetched");

queryClient.setQueryData("getOffersAdmin",data)
setOffers(data)
}),
onError: (error) => {
console.log(error);
}
},
)
const queryClient = useQueryClient()
const offersMutation = useMutation(
() => getOffers(),
{
onSuccess: (data => {
console.log("Fetched");

queryClient.setQueryData("getOffersAdmin",data)
setOffers(data)
}),
onError: (error) => {
console.log(error);
}
},
)
on my table action i tried that:
const handleDeletePost = (id: string) => {
const queryKeys = queryClient.getQueryCache().getAll().map(cache => JSON.parse(cache.queryHash))
console.log(queryKeys);
queryClient.refetchQueries({ queryKey: ["getOffersAdmin"] })

}
const handleDeletePost = (id: string) => {
const queryKeys = queryClient.getQueryCache().getAll().map(cache => JSON.parse(cache.queryHash))
console.log(queryKeys);
queryClient.refetchQueries({ queryKey: ["getOffersAdmin"] })

}
thank you
7 Replies
rival-black
rival-black2y ago
Read the documentation please
exotic-emerald
exotic-emeraldOP2y ago
I did it that's why i'm here now, don't be rude, if you know what i did wrong tell me so i don't make the same error again. Maybe i didn't understood the doc in the right way
rival-black
rival-black2y ago
Why are you using useMutation and not useQuery??
exotic-emerald
exotic-emeraldOP2y ago
because i saw in the documentation that we should use (as i understood) useMutation we we want to do something with the data after the request is executed
rival-black
rival-black2y ago
Oh boy. I recall the first response: read the docs
automatic-azure
automatic-azure2y ago
Use useQuery and you will find refetch to use it
exotic-emerald
exotic-emeraldOP2y ago
well thanks

Did you find this page helpful?