TanStackT
TanStackโ€ข2y agoโ€ข
1 reply
hidden-sapphire

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;


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 ๐Ÿ˜„
Was this page helpful?