Web Fundamentals

React Query (TanStack Query): Server State Caching

Web Fundamentals Map

Rendering & Browser Architecture

Critical Rendering PathScript Loading Patterns (async/defer)Event Loop Deep DiveJavaScript Module Systems (CJS, ESM, UMD)Dynamic Module Loading (import())Import on InteractionImport on VisibilityBrowser Rendering Pipeline & Layout ThrashingRendering Strategies (CSR, SSR, SSG, ISR)Streaming SSR & Progressive HTMLIslands ArchitectureReact Server ComponentsFramework Reactivity (React, Vue, Svelte, Solid)HTTP/1.1 vs HTTP/2 vs HTTP/3 (QUIC)DNS Resolution & TTL Caching

Performance

Core Web Vitals: LCP, INP, CLSPerformance Optimization Trade-offsCritical Resource PrioritizationCode Splitting & Dynamic ImportsTree Shaking & Dead Code EliminationLazy LoadingResource Hints: Preload, Prefetch & PreconnectText Compression: Gzip & BrotliImage & Video OptimizationAdaptive LoadingList VirtualizationWeb Workers vs Main ThreadMemory Leaks: Detection & PreventionManaging Third-Party ScriptsHow CDNs WorkHTTP Caching Deep DiveService Workers & Offline StrategyPWA Fundamentals

Security

Cross-Site Scripting (XSS)Cross-Site Request Forgery (CSRF)CORS ExplainedCORS Preflight, Credentials & MisconfigurationsContent Security Policy (CSP)Why is HTTPS Secure? (TLS/SSL)Authorization Best PracticesCookie Security & Session Hardening

State & Data Architecture

State Management Guide (Context vs Zustand vs Redux)React Query & Server State CachingData Fetching PatternsCaching StrategiesPagination: Offset vs Cursor-BasedReal-time Communication (WebSocket, SSE, Polling)
mediumWeb Fundamentals

React Query (TanStack Query): Server State Caching

Learn the interview-ready mental model, practical trade-offs, and production patterns for this web fundamentals topic.

Topic content

TL;DRReact Query owns server state. Use query keys, staleTime, invalidation, and optimistic updates for fast + correct UI.
Very High Signal
Google
Meta
Netflix
Agoda
30-Second Answerstart every interview with this

React Query (TanStack Query) is the standard solution for managing server state in React apps. It handles caching, request deduplication, background refetching, mutations, and optimistic updates with minimal boilerplate.

You tell it what data you want (query key). It fetches, caches, and keeps it fresh automatically. When you change data (mutation), it knows what to update or invalidate. You focus on UI; it handles the messy parts of server synchronization.

Define queryKey + queryFn
React Query handles fetching + caching
UI subscribes via useQuery
Mutations trigger smart invalidation/updates

1Query Keys & Basic Usage

Query keys are the identity of your cached data. Use stable, hierarchical arrays. React Query automatically dedupes and caches based on these keys.

useUser.tsts
const { data, isLoading } = useQuery({
  queryKey: ['user', userId],
  queryFn: () => fetchUser(userId),
  staleTime: 60_000
});

2Caching Strategy (staleTime, gcTime)

staleTime controls how long data is considered fresh. gcTime controls how long inactive queries stay in memory.

3Mutations & Optimistic Updates

Use useMutation with invalidateQueries for correctness or setQueryData for instant UI. Optimistic updates provide rollback on error.

4Pagination & Infinite Scroll

Keep pagination params in query keys. Use useInfiniteQuery for infinite scrolling with getNextPageParam.

Key Takeaways
  • ✓React Query is the standard for server state management
  • ✓Query keys are the foundation of caching and invalidation
  • ✓Use staleTime to control freshness, gcTime for memory
  • ✓invalidateQueries for correctness, setQueryData for instant UX
  • ✓Optimistic updates need proper rollback
  • ✓Separate server state (React Query) from client state (Zustand/Context)
  • ✓Always measure and tune based on real usage patterns
PreviousNext

Topic Guide

On this page