T
TanStack•17mo ago
optimistic-gold

query.data with initial value returns undefined

Hello, did not know where to post it exactly, as it seems like bug, however it is not reproducable so gonna ask here: We have a query like this:
const queryResult = useQuery({
queryKey: [SOME_UNIQUE_KEY, foo],
queryFn: () => doSomething(foo),
initialData: { isEnabled: false },
});

return queryResult.data.isEnabled;
const queryResult = useQuery({
queryKey: [SOME_UNIQUE_KEY, foo],
queryFn: () => doSomething(foo),
initialData: { isEnabled: false },
});

return queryResult.data.isEnabled;
It is wrapped around a function (hook) that is called directly in App.tsx, which means is called very often Very rarely (which unfortunately makes it unreproducible) we get a following error: Uncaught TypeError: Cannot read properties of undefined (reading 'isEnabled') TypeError: Cannot read properties of undefined (reading 'isEnabled') My question is, is it even possible for data to be undifined if initial data is provided? Shouldn't that be default value if query is not fetched yet? React-query version: v4.29.19 Typescript version: v4.8.4 Would be very grateful for response 😄
1 Reply
fair-rose
fair-rose•17mo ago
if doSomething(foo) returns undefined for some reason, then you'll have overwritten the initialData with that. Maybe doSomething catches errors and doesn't reject promises? https://tkdodo.eu/blog/react-query-fa-qs#logging in v5, we warn if undefined is returned from the queryFn
React Query FAQs
Answering the most frequently asked React Query questions

Did you find this page helpful?