liveBook · Manning

getQueryData

As explained in React Hooks in Action: With Suspense and Concurrent Mode

getQueryData is a React Query query-client method that returns previously cached data for a query key.

getQueryData reads previously fetched data from the React Query cache through a query client. In an edit form, it can reuse a bookable already loaded by the list page instead of immediately showing a loading spinner. section 10.3.4

Populate an edit form from cached data

Call useQueryClient to obtain the client, then pass the relevant query key to getQueryData. To select one bookable, find its ID in the cached list: const queryClient = useQueryClient(); const bookables = queryClient.getQueryData("bookables"); const bookable = bookables?.find(b => b.id === id); This value can supply useQuery's initialData. When it is present, the first query call can avoid a server fetch; when it is undefined, such as after direct navigation, useQuery fetches the data. section 10.3.4

const bookables = queryClient.getQueryData("bookables");

Shows the direct query-client call used to retrieve the cached bookables list. section 10.3.4

How React Hooks in Action: With Suspense and Concurrent Mode covers it

How deep it goes
Explains client access, keyed lookup, and initialData behavior, but stops short of cache-expiry or mutation internals.
Examples
Demonstrates populating BookableEdit from the cached bookables list and selecting a record with find.
What it assumes
Assumes a configured React Query client and familiarity with useQuery, query keys, optional chaining, and array find.
What you can do after
Apply it via useQueryClient to seed useQuery with cached data and reduce unnecessary loading during navigation.
How it connects
Connects React Query, query cache, useQueryClient, useQuery, query keys, and initialData.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of React Hooks in Action: With Suspense and Concurrent Mode. The section links point to the parts of the book it draws on.