Capítulo 1 de 80
TanStack Query (formerly React Query) is a server-state library: it fetches, caches, synchronizes and updates data that lives on a server, which behaves fundamentally differently from client state (it's owned elsewhere, can go stale, and must be fetched/updated asynchronously).
QueryClient + QueryClientProvider own the cache; useQuery reads/subscribes to a cache entry keyed by a queryKey; useMutation performs writes.import { QueryClient, QueryClientProvider, useQuery } from '@tanstack/react-query'
const queryClient = new QueryClient()
export default function App() {
return (
<QueryClientProvider client={queryClient}>
<Example />
</QueryClientProvider>
)
}
function Example() {
const { isPending, error, data } = useQuery({
queryKey: ['repoData'],
queryFn: () => fetch('https://api.github.com/repos/TanStack/query').then((res) => res.json()),
})
if (isPending) return 'Loading...'
if (error) return 'An error has occurred: ' + error.message
return <div>{data.name}</div>
}
QueryClient at the root, one useQuery call per piece of server data, and the isPending/error/data triad every query result exposes.useState/Redux/Zustand.isPending/error/data is the baseline shape of a query result; later chapters cover the fuller status model.useQuery and the cache-key model introduced here.