📁 Sangam Dhakal - Portfolio v2.0 - Netscape Navigator 4.0
_
□
×

📚 Interview question

When would you use TanStack Query instead of fetching in useEffect, and what does it give you?

Junior Theory Beginner

🎯 Key points

  • Caches by query key and de-duplicates requests
  • Retries, background refetch on focus/reconnect, stale-while-revalidate
  • Polling with refetchInterval; pagination and infinite scroll with useInfiniteQuery
  • invalidateQueries or setQueryData after a mutation; optimistic updates with rollback
  • Passes an AbortSignal to queryFn, removing race conditions from hand-written effects
  • Devtools and query defaults make behaviour consistent across screens
  • Not a replacement for server-side validation, authorisation or HTTP caching

⏱ 30-second answer

TanStack Query treats API data as server state: it caches responses by a query key, de-duplicates identical requests, retries failures, refetches stale data on window focus or reconnect, supports polling (refetchInterval) and paginated / infinite queries (placeholderData: keepPreviousData, useInfiniteQuery), and lets you invalidate keys after a mutation so lists refresh. Hand-written useEffect fetches repeat all of that per screen and usually get races and cleanup wrong, so the library mostly buys developer time and fewer bugs.

Use plain fetch for one or two calls; use TanStack Query once several screens share data, you page through large lists, or you need optimistic updates. It does not replace the API's own HTTP caching or authorisation.

🔍 What the interviewer is checking

Checks whether they understand server-state caching, de-duplication and invalidation.

↩ Usually asked after

More in SPA Integration

Last updated 23 Nov 2026.

🌐 Sangam's Webring

⏪ Previous 🏠 Main Next ⏩
💬 Chat with me