npm install @tanstack/react-query @tanstack/react-query-devtools
Wrap your app with QueryClientProvider and build a data-fetching component.
1 'use client'; 2 import { QueryClient, QueryClientProvider } from '@tanstack/react-query'; 3 import { ReactQueryDevtools } from '@tanstack/react-query-devtools'; 4 import { useState } from 'react'; 5 6 export function Providers({ children }: { children: React.ReactNode }) { 7 const [queryClient] = useState(() => new QueryClient({ 8 defaultOptions: { 9 queries: { 10 staleTime: 60 * 1000, // 1 minute 11 retry: 1, 12 }, 13 }, 14 })); 15 16 return ( 17 <QueryClientProvider client={queryClient}> 18 {children} 19 <ReactQueryDevtools initialIsOpen={false} /> 20 </QueryClientProvider> 21 ); 22 }
Users fetch with loading state, cache shared across components, DevTools show query state
Sign in to share your feedback and join the discussion.