import { useMutation, useQueryClient } from "@tanstack/react-query";
import api from "./Api";
export const usePostProduct = () => {
const qc = useQueryClient();
return useMutation({
mutationFn: async (newProduct) => {
const res = await api.post("/products", newProduct);
return res.data;
},
onSuccess: () => {
qc.refetchQueries(["products"]);
},
});
};
I have two pages: All Products
and Upload Product
. I’m using React Query to fetch products after posting a new product. Everything works well when open “All Products” page before “Upload Product” page, but when I open the ‘Upload Product’ page and post a product before opening the “All Products” page, it doesn’t fetch the product as expected. It only fetches the product after I open the “All Products” page.
What I want is to fetch all products after posting a new product, regardless of whether I’ve opened the “All Products” page beforehand or not.