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:
<script>import { useQuery } from '@sveltestack/svelte-query';const queryResult = useQuery('todos', fetchTodos)</script>{#if $queryResult.status === 'loading'}<span>Loading...</span>{:else if $queryResult.status === 'error'}<span>Error: {$queryResult.error.message}</span>{:else}{#if $queryResult.isFetching}<div>Refreshing...</div>{/if}{#each $queryResult.data as todo}<Todo {todo} />{/each}{/if}
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:
<script>import { useIsFetching } from '@sveltestack/svelte-query'const isFetching = useIsFetching()</script>{#if $isFetching}<div>Queries are fetching in the background...</div>{/if}
The latest TanStack news, articles, and resources, sent to your inbox.