Skip to content

Instantly share code, notes, and snippets.

View luillyfe's full-sized avatar
💭
Improving

Fermin Blanco luillyfe

💭
Improving
View GitHub Profile
@luillyfe
luillyfe / reactRouterLoaderQuery.ts
Created November 30, 2023 14:40
React router loader being cached
export function todosLoader(queryClient: QueryClient) {
return async function (): Promise<{ todos: Task[] }> {
const { queryKey, queryFn } = todosQuery();
// It does the trick for returning any data we have in the cache, even if it's stale.
// If the query does not exist, queryClient.fetchQuery will be called and its results returned.
const todos = (await queryClient.ensureQueryData({
queryKey,
queryFn,
})) as Task[];