--- id: quick-start title: Quick Start --- The `@tanstack/svelte-query` package offers a 1st-class API for using TanStack Query via Svelte. ## Example ```svelte ``` Then call any function (e.g. createQuery) from any component: ```svelte
{#if query.isPending}

Loading...

{:else if query.isError}

Error: {query.error.message}

{:else if query.isSuccess} {#each query.data as todo}

{todo.title}

{/each} {/if}
``` ## Important Differences between Svelte Query & React Query Svelte Query offers an API similar to React Query, but there are some key differences to be mindful of. - Arguments to `svelte-query` primitives (like `createQuery`, `createMutation`) are functions, so that they can be tracked in a reactive scope. ```ts // ❌ react version useQuery({ queryKey: ['todos', todo], queryFn: fetchTodos, }) // ✅ svelte version createQuery(() => ({ queryKey: ['todos', todo], queryFn: fetchTodos, })) ``` - Svelte Query primitives do not support destructuring. The return value from these functions is a proxy, and their properties are dynamically resolved. ```svelte
{#if query.isPending}

Loading...

{:else if query.isError}

Error: {query.error.message}

{:else if query.isSuccess}

{query.data.name}

{query.data.description}

👀 {query.data.subscribers_count} ✨ {query.data.stargazers_count} 🍴 {query.data.forks_count}
{/if}
``` - Runes values can be passed in directly to function arguments. Svelte Query will update the query automatically. ```svelte
{#if todosQuery.isPending}

Loading...

{:else if todosQuery.isError}

Error: {todosQuery.error.message}

{:else if todosQuery.isSuccess} {#each todosQuery.data as todo} {/each} {/if}
``` - Errors can be caught and reset using Svelte's native `` component. Set `throwOnError` option to `true` to make sure errors are thrown to the `` component. - Since property tracking is handled through Svelte's fine-grained reactivity, options like `notifyOnChangeProps` are not needed