Blog>
Snippets

Automatically Refetching on Network Reconnect

Provide an example of configuring React Query to automatically refetch data when the user's network status changes from offline to online.
import { QueryClient, QueryClientProvider } from 'react-query';

// Create a client
const queryClient = new QueryClient({
  defaultOptions: {
    queries: {
      refetchOnReconnect: true // Automatically refetch on reconnect
    }
  }
});

function App() {
  return (
    <QueryClientProvider client={queryClient}>
      {/* The rest of your application */}
    </QueryClientProvider>
  );
}
This code snippet is setting up React Query in a React application. It creates a new QueryClient and configures it to automatically refetch data for all queries when the network connection is reestablished. This is done by setting the refetchOnReconnect option to true. Finally, the QueryClientProvider wraps the application, providing the configured client to all React Query hooks used within.