Blog>
Snippets

Initializing TanStack QueryClient

Demonstrate the setup of QueryClient and its configuration for cache time and query retries.
import { QueryClient } from '@tanstack/react-query';
First, import the QueryClient constructor from '@tanstack/react-query' to be able to create a new instance.
const queryClient = new QueryClient({
  defaultOptions: {
    queries: {
      cacheTime: 1000 * 60 * 5, // 5 minutes in milliseconds
      retry: 3, // Retry failed queries up to 3 times
    },
  },
});
Create a new instance of QueryClient and configure it with a cache time of 5 minutes and to retry failed queries up to 3 times. These settings apply to all queries managed by this QueryClient instance.
export { queryClient };
Export the configured QueryClient instance for use throughout your application.