T
TanStack•2y ago
national-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
national-gold
national-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);
};
vicious-gold
vicious-gold•2y ago
Can you please post a reproduction?
national-gold
national-goldOP•2y ago
Screen recording?
vicious-gold
vicious-gold•2y ago
😅 no Codesandbox, stackblitz, …
national-gold
national-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?
vicious-gold
vicious-gold•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
national-gold
national-goldOP•2y ago
Yep, I'm preparing the sandbox
wise-white
wise-white•2y ago
there's nothing wrong with the code here. seems like a backend issue, not RQ related

Did you find this page helpful?