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() |
Invest now in Acorns!!! 🚀
Join Acorns and get your $5 bonus!
Acorns is a micro-investing app that automatically invests your "spare change" from daily purchases into diversified, expert-built portfolios of ETFs. It is designed for beginners, allowing you to start investing with as little as $5. The service automates saving and investing. Disclosure: I may receive a referral bonus.
Invest now!!! Get Free equity stock (US, UK only)!
Use Robinhood app to invest in stocks. It is safe and secure. Use the Referral link to claim your free stock when you sign up!.
The Robinhood app makes it easy to trade stocks, crypto and more.
Webull! Receive free stock by signing up using the link: Webull signup.
More Related questions...
