T
TanStack•2y ago
stormy-gold

Why do PATCH and DELETE requests not refetch the data from the database?

Hello! When I store data in the database, I usually do it the following way:
const createPost = useMutation(
(data) => {
return api.storePost({ data });
},
{
onSuccess: () => {
queryClient.invalidateQueries(["posts"]);
toast.success("Post created");
},
}
);
const createPost = useMutation(
(data) => {
return api.storePost({ data });
},
{
onSuccess: () => {
queryClient.invalidateQueries(["posts"]);
toast.success("Post created");
},
}
);
But whenever I delete or patch like this:
const editPost = useMutation(
(data) => {
return api.editPost({ data });
},
{
onSuccess: () => {
queryClient.invalidateQueries(["posts"]);
toast.success("Post edited successfully");
},
}
);
const editPost = useMutation(
(data) => {
return api.editPost({ data });
},
{
onSuccess: () => {
queryClient.invalidateQueries(["posts"]);
toast.success("Post edited successfully");
},
}
);
React query does not re-fetch the data, why?
8 Replies
stormy-gold
stormy-goldOP•2y ago
its literally the same thing, all that changes is in the services.jsx file, where I handle delete or patch requests the following way:
export const deletePost = (post) => {
api.delete(`/post/delete/${post.data}`).then((res) => res.data);
};
export const deletePost = (post) => {
api.delete(`/post/delete/${post.data}`).then((res) => res.data);
};
correct-apricot
correct-apricot•2y ago
Can you please post a reproduction?
stormy-gold
stormy-goldOP•2y ago
Screen recording?
correct-apricot
correct-apricot•2y ago
😅 no Codesandbox, stackblitz, …
stormy-gold
stormy-goldOP•2y ago
oh dang, that would take some time, but I'll try. I noticed it does refetch, but it takes quite some time for some reason is there any way to actually make it faster?
correct-apricot
correct-apricot•2y ago
Yea buy more internet Just kidding. It is hard to help you without knowing your code. Yes. This takes time. But makes answering your question much more efficient
stormy-gold
stormy-goldOP•2y ago
Yep, I'm preparing the sandbox
sunny-green
sunny-green•2y ago
there's nothing wrong with the code here. seems like a backend issue, not RQ related

Did you find this page helpful?