TanStack Query (React Query): Handling HTTP Requests with EaseEntry 10 of 10
Summary
| Concept | TanStack Query (React) | Angular (built-in) |
|---|---|---|
| Setup | QueryClient + <QueryClientProvider> | provideHttpClient() |
| Read data | useQuery({ queryKey, queryFn }) | rxResource({ params, stream }) / httpResource() |
| State | data, isPending, isLoading, isError, error | value(), isLoading(), error(), status() |
| Manual refetch | refetch() | reload() |
| Cache | QueryClient, keyed by queryKey | Root service with signals |
| Freshness | staleTime | Timestamp check in service |
| Cache cleanup | gcTime | Service lifetime |
| Parameters | Dynamic queryKey | params signal |
| Cancellation | signal → fetch | Automatic (unsubscribe) / abortSignal |
| Conditional | enabled | params returns undefined |
| Write data | useMutation + mutate() | Service call + subscribe() |
| After write | invalidateQueries({ queryKey }) | Version signal / reload() |
| Optimistic | onMutate + setQueryData + rollback in onError | value.set() + rollback in error |
| Final resync | onSettled → invalidateQueries | finalize → reload() |
TanStack Query turns server data into a shared, self-refreshing cache: queries read by key, mutations write and invalidate, and optimistic updates edit the cache directly with a rollback plan. Angular covers the same needs with built-in pieces: HttpClient services for requests, rxResource() for signal-based request state, and root services with signals when data must be shared, cached, or updated optimistically.