Background Fetching Indicators

A query's status === 'loading' state is sufficient enough to show the initial hard-loading state for a query, but sometimes you may want to display an additional indicator that a query is refetching in the background. To do this, queries also supply you with an isFetching boolean that you can use to show that it's in a fetching state, regardless of the state of the status variable:

import { useQuery } from '@sveltestack/svelte-query';
const queryResult = useQuery('todos', fetchTodos)
{#if $queryResult.status === 'loading'}
{:else if $queryResult.status === 'error'}
<span>Error: {$queryResult.error.message}</span>
{#if $queryResult.isFetching}
{#each $ as todo}
<Todo {todo} />

Displaying Global Background Fetching Loading State

In addition to individual query loading states, if you would like to show a global loading indicator when any queries are fetching (including in the background), you can use the useIsFetching hook:

import { useIsFetching } from '@sveltestack/svelte-query'
const isFetching = useIsFetching()
{#if $isFetching}
<div>Queries are fetching in the background...</div>
Was this page helpful?

Subscribe to our newsletter

The latest TanStack news, articles, and resources, sent to your inbox.

    I won't send you spam.

    Unsubscribe at any time.

    © 2020 Tanner Linsley. All rights reserved.