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

The isRefetching Flag

Distinguishing between initial loading and background refetching in your UI.

The isRefetching Flag

You'll learn how to distinguish between initial loading states and background refetch states in your UI.

When you fetch data with TanStack Query, queries can load in two different contexts: the very first time (no cached data exists), or in the background while you already have cached data displayed. The isRefetching flag helps you tell these scenarios apart.

The Two Loading States

useQuery returns two boolean flags:

  • isLoading: True only during the initial fetch when no cached data exists yet
  • isRefetching: True whenever a fetch is happening in the background while cached data is already available
const { data, isLoading, isRefetching } = useQuery({
  queryKey: ['todos'],
  queryFn: fetchTodos
});

if (isLoading) {
  // No data yet, first load
  return <Spinner />;
}

return (
  <div>
    {isRefetching && <div className="refetch-badge">Updating...</div>}
    <TodoList todos={data} />
  </div>
);

Why This Matters

Imagine checking your email: the first time you open the app, you see a full-screen spinner (initial load). But when you refresh while emails are already visible, you see a small indicator in the corner—you don't want the entire UI to disappear.

isRefetching lets you show subtle UI hints (a badge, a spinning icon) without disrupting the user's view of the existing cached data. Meanwhile, isLoading handles the "nothing here yet" state with a proper loading screen.

Key Takeaway: Use isLoading for initial empty states and isRefetching to show background update indicators while keeping cached data visible.