Typing of data for useQuery when placeholder data is set?
Something I noticed recently is that when using placeholder data, the return type of useQuery in the react package is undefined. This seems counter intuitive? I would expect that with a static object in placeholderData that the return type of data would be defined. Further, in my very basic testing it is always defined?
TS Playground
TS Playground - An online editor for exploring TypeScript and JavaS...
The Playground lets you write TypeScript or JavaScript online in a safe and sharable way.


3 Replies
correct-apricot•4w ago
you can use initialData or check for isPlaceholderData first
not sure if it's intentional design decision
other-emerald•4w ago
placeholderData does not go into the cache and it actually reverts back to undefined at runtime when the query errors. See: https://tkdodo.eu/blog/placeholder-and-initial-data-in-react-query
Placeholder and Initial Data in React Query
Learn about the different possibilities to avoid loading spinners in React Query.
plain-purpleOP•4w ago
Thank you that makes sense
"it actually reverts back to undefined at runtime when the query errors" this is the key part