T
TanStack3y ago
correct-apricot

Is `data` always meant to be possibly undefined

Even when using even when using keepPreviousData and placeholderData?
const { data } = useQuery({
queryKey: taskBoardQueries.weeklyHours({ start }).queryKey,
queryFn: async () => {
const response = await taskBoardClient.getWeeklyBillableHours({
start: start.value,
});

const schema = z.object({
hours: z.number(),
target: z.number().nullable(),
});

return schema.parse(response);
},
placeholderData: {
target: 0,
hours: 0,
},
keepPreviousData: true,
});
const { data } = useQuery({
queryKey: taskBoardQueries.weeklyHours({ start }).queryKey,
queryFn: async () => {
const response = await taskBoardClient.getWeeklyBillableHours({
start: start.value,
});

const schema = z.object({
hours: z.number(),
target: z.number().nullable(),
});

return schema.parse(response);
},
placeholderData: {
target: 0,
hours: 0,
},
keepPreviousData: true,
});
Should data.value (Vue) be possible undefined in this case? As far as I know, with this set up, it will never not be defined right?
3 Replies
extended-salmon
extended-salmon3y ago
yes, placeholderData is no guarantee that there will always be data. if the query fails, data will be undefined: 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.
correct-apricot
correct-apricotOP3y ago
Ah okay that makes sense. So really there's no way to ensure the data is always defined is there? Just trying to battle best way where I had a dsahboard with heaps of async calls to stats (10+ endpoints) and all that data loads at different times, but if I use some placeholder data, my graphs etc still populate and I don't get all the jumping around Except I still get a litany of TS errors
extended-salmon
extended-salmon3y ago
it is defined if you set initialData

Did you find this page helpful?