Blog>
Snippets

Setting Up React Query in Your Project

Demonstrate initializing React Query in a React application by creating a QueryClient and wrapping the application's root component with QueryClientProvider.
import { QueryClient, QueryClientProvider } from 'react-query';
Import QueryClient and QueryClientProvider from react-query.
const queryClient = new QueryClient();
Create a new instance of QueryClient. This will manage the cache, queries, and mutations.
function App() {
  return (
    <QueryClientProvider client={queryClient}>
      {/* Your application components */}
    </QueryClientProvider>
  );
}
Wrap your application's root component in the QueryClientProvider, passing the queryClient instance. This provides React Query functionality throughout your app.