Setting Up React Query & QueryClient
Install TanStack Query, wrap your app in QueryClientProvider, and run your first useQuery.
Setting Up React Query & QueryClient is a free React Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the React Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Welcome
What Is React Query?
Installation
npm install @tanstack/react-query
npm install @tanstack/react-query-devtools --save-devCreating a QueryClient
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
const queryClient = new QueryClient();
createRoot(document.getElementById('root')).render(
<QueryClientProvider client={queryClient}>
<App />
</QueryClientProvider>
);Your First useQuery
import { useQuery } from '@tanstack/react-query';
function Posts() {
const { data, isLoading, isError } = useQuery({
queryKey: ['posts'],
queryFn: () => fetch('/api/posts').then(r => r.json()),
});
if (isLoading) return <p>Loading...</p>;
if (isError) return <p>Error!</p>;
return <ul>{data.map(p => <li key={p.id}>{p.title}</li>)}</ul>;
}Query Keys
// All posts
useQuery({ queryKey: ['posts'], queryFn: fetchPosts })
// Post with id 5
useQuery({ queryKey: ['posts', 5], queryFn: () => fetchPost(5) })
// Posts filtered by category
useQuery({ queryKey: ['posts', { category: 'react' }], queryFn: /* ... */ })Query Status
React Query DevTools
import { ReactQueryDevtools } from '@tanstack/react-query-devtools';
<QueryClientProvider client={queryClient}>
<App />
<ReactQueryDevtools initialIsOpen={false} />
</QueryClientProvider>Default Configuration
const queryClient = new QueryClient({
defaultOptions: {
queries: {
staleTime: 60_000, // data is fresh for 60s
retry: 2, // retry failed queries twice
refetchOnWindowFocus: false,
},
},
});Automatic Refetching
Quick Check
Recap
Up Next
Frequently asked questions
Is the “Setting Up React Query & QueryClient” lesson free?
Yes — the full text of “Setting Up React Query & QueryClient” is free to read here on the web, and the React Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the React Academy course, upgrade to CoddyKit PRO.
What will I learn in “Setting Up React Query & QueryClient”?
Install TanStack Query, wrap your app in QueryClientProvider, and run your first useQuery. You practise React Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start React Academy?
No prior experience is required. React Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Setting Up React Query & QueryClient” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this React Academy lesson?
Yes. Every React Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Setting Up React Query & QueryClient
- Caching, Stale Time & Background Refetching
- Mutations with useMutation
- Infinite Scroll & Pagination with useInfiniteQuery