What is React Query (TanStack Query)?

Answer

TanStack Query (formerly React Query) is a powerful data fetching and synchronization library that manages server state — data that comes from an API and needs to be kept in sync. Key features: Automatic caching: query results are cached by query key. Background refetching: data is refreshed in the background when the window regains focus, on a schedule, or when stale. Loading/error states: isLoading, isError, data from useQuery. Mutations: useMutation for create/update/delete. Cache invalidation: queryClient.invalidateQueries to refetch after a mutation. Usage: const { data, isLoading, error } = useQuery({ queryKey: ["user", id], queryFn: () => fetchUser(id) }). React Query distinguishes server state (remote, async, potentially stale) from client state (local, synchronous). Many apps using Redux for API data can replace it with React Query + a minimal local state solution (Zustand/Context), reducing complexity dramatically.