|
1 | 1 | ---
|
2 | 2 | id: overview
|
3 |
| -title: Svelte Query (Coming Soon) |
| 3 | +title: Svelte Query |
4 | 4 | ---
|
5 | 5 |
|
6 |
| -> ⚠️ This module has not yet been developed. It requires an adapter similar to `react-query` to work. We estimate the amount of code to do this is low-to-moderate, but does require familiarity with the Svelte framework. If you would like to contribute this adapter, please open a PR! |
| 6 | +The `@tanstack/svelte-query` package offers a 1st-class API for using TanStack Query via Svelte. |
7 | 7 |
|
8 |
| -The `@tanstack/svelte-query` package offers a 1st-class API for using TanStack Query via Svelte. However, all of the primitives you receive from this API are core APIs that are shared across all of the TanStack Adapters including the Query Client, query results, query subscriptions, etc. |
| 8 | +## Example |
| 9 | + |
| 10 | +Include the QueryClientProvider near the root of your project: |
| 11 | + |
| 12 | +```svelte |
| 13 | +<script lang="ts"> |
| 14 | + import { QueryClientProvider, QueryClient } from '@tanstack/svelte-query' |
| 15 | + import Simple from './lib/Example.svelte' |
| 16 | +
|
| 17 | + const queryClient = new QueryClient() |
| 18 | +</script> |
| 19 | +
|
| 20 | +<QueryClientProvider client={queryClient}> |
| 21 | + <Simple /> |
| 22 | +</QueryClientProvider> |
| 23 | +``` |
| 24 | + |
| 25 | +Then call any function (e.g. createQuery) from any component: |
| 26 | + |
| 27 | +```svelte |
| 28 | +<script lang="ts"> |
| 29 | + import { createQuery } from '@tanstack/svelte-query' |
| 30 | +
|
| 31 | + const query = createQuery({ |
| 32 | + queryKey: ['todos'], |
| 33 | + queryFn: () => fetchTodos(), |
| 34 | + }) |
| 35 | +</script> |
| 36 | +
|
| 37 | +<div> |
| 38 | + {#if $query.isLoading} |
| 39 | + <p>Loading...</p> |
| 40 | + {:else if $query.isError} |
| 41 | + <p>Error: {$query.error.message}</p> |
| 42 | + {:else if $query.isSuccess} |
| 43 | + {#each $query.data as todo} |
| 44 | + <p>{todo.title}</p> |
| 45 | + {/each} |
| 46 | + {/if} |
| 47 | +</div> |
| 48 | +``` |
| 49 | + |
| 50 | +## Available Functions |
| 51 | + |
| 52 | +Svelte Query offers useful functions and components that will make managing server state in Svelte apps easier. |
| 53 | + |
| 54 | +- `createQuery` |
| 55 | +- `createQueries` |
| 56 | +- `createInfiniteQuery` |
| 57 | +- `createMutation` |
| 58 | +- `useQueryClient` |
| 59 | +- `useIsFetching` |
| 60 | +- `useIsMutating` |
| 61 | +- `useHydrate` |
| 62 | +- `<QueryClientProvider>` |
| 63 | +- `<Hydrate>` |
| 64 | + |
| 65 | +## Important Differences between Svelte Query & React Query |
| 66 | + |
| 67 | +Svelte Query offers an API similar to React Query, but there are some key differences to be mindful of. |
| 68 | + |
| 69 | +- Many of the functions in Svelte Query return a Svelte store. To access values on these stores reactively, you need to prefix the store with a `$`. You can learn more about Svelte stores [here](https://svelte.dev/tutorial/writable-stores). |
0 commit comments