Web / TanStack Interview Questions
What is TanStack Query and what core problems does it solve over plain fetch in Vue?
TanStack Query wraps async data-fetching functions and provides automatic caching, background refetching, request deduplication, loading/error state, and cache invalidation — with zero manual cache code.
<!-- WITHOUT TanStack Query --> <script setup lang="ts"> import { ref, onMounted } from "vue" const user=ref(null), loading=ref(true), error=ref(null) onMounted(async()=>{ try{ user.value=await fetch("/api/user/1").then(r=>r.json()) } catch(e){ error.value=e } finally{ loading.value=false } }) </script> <!-- WITH TanStack Query --> <script setup lang="ts"> import { useQuery } from "@tanstack/vue-query" const { data:user, isPending, isError } = useQuery({ queryKey: ["user",1], queryFn: ()=>fetch("/api/user/1").then(r=>r.json()), }) </script>
| Concern | Plain fetch | TanStack Query |
|---|---|---|
| Loading state | Manual ref(true/false) | isPending, isFetching auto-managed |
| Error state | Manual try/catch | isError, error auto-managed |
| Caching | None — re-fetches every mount | Cached by queryKey, configurable TTL |
| Deduplication | Duplicate concurrent requests | Single in-flight request per key |
| Background refresh | None | Auto on window focus / reconnect |
| Invalidation | Manual | queryClient.invalidateQueries() |
More Related questions...