Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445import { useQuery, QueryClient } from "@tanstack/react-query";
const partialQuery = (id: number) => { return { queryKey: ["pokemon", id], runQuery: () => fetch(`https://pokeapi.co/api/v2/pokemon/${id}`).then((res) => res.json() ), };};
const queryRegistry = { partialQuery,} satisfies Record< string, (...args: any[]) => { queryKey: unknown[]; runQuery: () => Promise<unknown> }>;
const getClientQuery = (registryKey: keyof typeof queryRegistry) => { const registryEntry = queryRegistry[registryKey]; return (...args: Parameters<typeof registryEntry>) => { const item = registryEntry(...args); return { queryKey: item.queryKey, runQuery: () => { item.runQuery(); }, }; };};
// Pass to any react-query hook/functionconst _pokemon = useQuery(partialQuery());
const queryClient = new QueryClient();queryClient.prefetchQuery(partialQuery());
// easily override as neededconst shouldGetPokemon = false;const _pokemon2 = useQuery({ ...partialQuery(), enabled: shouldGetPokemon,});