Course contentsShow
← React
Lesson 1785 of 2,39642. Data Fetching LibrariesFree lesson

Automatic Refetch on Network Reconnect

Refetching stale queries automatically when the network connection is restored.

Automatic Refetch on Network Reconnect

You'll learn how TanStack Query automatically refetches stale queries when your internet connection comes back online.

The Real-World Problem

Imagine you're browsing a weather app on your phone in the subway. You lose signal, then emerge above ground with connection restored. Without automatic refetching, you'd still see old weather data from before you went underground — even though your app is now online and could fetch fresh information.

How TanStack Query Handles This

By default, TanStack Query listens for network reconnection events. When your device goes from offline to online, it automatically refetches any queries marked as stale — just like it does with window focus refetching.

This means your users always see the most current data after network disruptions, without clicking a refresh button or reloading the page.

The Configuration

const { data } = useQuery({
  queryKey: ['weather', city],
  queryFn: fetchWeather,
  refetchOnReconnect: true, // default: true
  staleTime: 60000 // Remember: only STALE queries refetch
});

When refetchOnReconnect is true (the default), TanStack Query will:

  1. Detect when the browser's online event fires
  2. Check which queries are currently stale
  3. Automatically refetch those queries
  4. Update your components with fresh data

Like window focus refetching, only stale queries refetch. If your data is still fresh (within staleTime), no unnecessary request happens.

Disabling When Needed

refetchOnReconnect: false // Opt out if you have a reason

You might disable this for data that doesn't change server-side, or when you're implementing custom reconnection logic.

Key Takeaway: TanStack Query's automatic refetch on network reconnect ensures users see current data after connectivity issues, with zero extra code — just smart defaults respecting your staleness configuration.