Initial data not being used
Hello 👋
any ideas why the initial data are not being used in the code below.
I have checked that data are in the cache but request happens regardless. thank you
I have checked that data are in the cache but request happens regardless. thank you

11 Replies
rare-sapphire•3y ago
Hi. You seem to be using as initial data the cached of the query. This is the default behavior to me. Maybe what you are missing is a staleTime so that data remains fresh and bus returned by the query of any
harsh-harlequinOP•3y ago
Yes that what the idea.
Get the initial data from the cache if there are any, otherwise call local useQuery
So I should add staleTime to initial query?
rare-sapphire•3y ago
Question: what does initial load mean to you? Query cache is an in memory cache. So on page refresh you would never have cached data. The only way is to use a persister (to local storage for ex) so that you could restore from there the previous data.
harsh-harlequinOP•3y ago
For example that
So the initialData inside the useQuery below should be truthy hence the useQuery should not have been called here since the there are initial data but it does ( 😄 sorry for my english)
const initialData in the screenshot already has the data needed ( i can log it) and gets that from the cache, right?So the initialData inside the useQuery below should be truthy hence the useQuery should not have been called here since the there are initial data but it does ( 😄 sorry for my english)

harsh-harlequinOP•3y ago
What I am trying to do is get the data for this component from the query cache and if there aren't any then call useQuery
rare-sapphire•3y ago
The const gets the data from the query cache. On first load the cache is empty so no initial data. The query is then created with no initial data. And since no staleTime the queryFn is called to retrieve the fresh ones. Once done at every render the I initial data const would get the the query data which is therefore no more « initial ». What are you trying to achieve?
harsh-harlequinOP•3y ago
I just dont want to make the same request for data that are already in the cache
I want to grab them from the query cache and if there arent any data in the cache then call useQuery
rare-sapphire•3y ago
Ok then staleTime is the way to go
harsh-harlequinOP•3y ago
Can I add staleTime in useMutation though?

rare-sapphire•3y ago
I don’t think so but you should be able to use dataUpdatedAt when setting data in the cache
harsh-harlequinOP•3y ago
thank you glabat 🙏 🤘