I'm using react query, suppose I have to use some default data. Whenever I have the 'initialData', the queryFn is not being called, hence onSucess is not being called as well. So, the data of that useQuery is still the same of the 'initialData' I have implemented.
const getProducts = () => {
console.log("from getProducts");
//Fetch data
}
export const useProducts = () =>
useQuery({
queryKey: ["AllProducts"],
cacheTime: Infinity,
staleTime: Infinity,
initialData: {
products: [],
},
queryFn: () => getProducts(),
onSuccess: (data) => {
console.log("from onSuccess");
// Do some stuff
}
The weird is neither console.log("from getProducts"); nor console.log("from onSuccess"); are being called. I know that I can use 'placeholder' instead of 'initialData'. But the question is why does this happen?
Note that these are the versions of react-query and react-query dev tools:
"@tanstack/react-query": "^4.29.12",
"@tanstack/react-query-devtools": "^4.29.12",
I have used 'placeholder' instead of 'initialData' and it worked.