Layout component fetching useQuery and child component
Heyo, super happy that Tanstack exists, I'm still learning it.
I have a _layout route component in tanstack that uses a query to fetch data
and then for the child component I want to use the same query to use the cached data.
Do I really have to declare the queryFn in the child pages when that function is set in the layout component?
I just want to read from the cache for the child components and then have the layout component fetch the data with the useQuery.
The inspector complains about the queryFn missing in the childPages useQueries.

2 Replies
like-goldOP•15mo ago
Ok it works if I wait for !isLoading before I render the Child Pages.
But I'd like to render them before, will have to investigate .. 🙂
Solved it by using the same queryFn in both parent and child, but setting enabled: false in the child component.
conscious-sapphire•15mo ago
Maybe you're better off using this https://tanstack.com/query/latest/docs/reference/QueryClient#queryclientgetquerydata
QueryClient | TanStack Query Docs
QueryClient
The QueryClient can be used to interact with a cache: