Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 4.2k
docs(svelte-query/quick-start): Add quick start docs#11226
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,151 @@ | ||
| --- | ||
| id: quick-start | ||
| title: Quick Start | ||
| --- | ||
| The `@tanstack/svelte-query` package offers a 1st-class API for using TanStack Query via Svelte. | ||
| ## Example | ||
| ```svelte | ||
| <script lang="ts"> | ||
| import { QueryClient, QueryClientProvider } from '@tanstack/svelte-query' | ||
| import Example from './lib/Example.svelte' | ||
| const queryClient = new QueryClient() | ||
| </script> | ||
| <QueryClientProvider client={queryClient}> | ||
| <Example /> | ||
| </QueryClientProvider> | ||
| ``` | ||
| Then call any function (e.g. createQuery) from any component: | ||
| ```svelte | ||
| <script lang="ts"> | ||
| import { createQuery } from '@tanstack/svelte-query' | ||
| const query = createQuery(() => ({ | ||
| queryKey: ['todos'], | ||
| queryFn: () => fetchTodos(), | ||
| })) | ||
| </script> | ||
| <div> | ||
| {#if query.isPending} | ||
| <p>Loading...</p> | ||
| {:else if query.isError} | ||
| <p>Error: {query.error.message}</p> | ||
| {:else if query.isSuccess} | ||
| {#each query.data as todo} | ||
| <p>{todo.title}</p> | ||
| {/each} | ||
| {/if} | ||
| </div> | ||
| ``` | ||
| ## 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`, `useIsFetching`) 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 store, and their properties are only tracked in a reactive context. | ||
| ```svelte | ||
| <script lang="ts"> | ||
| import { createQuery } from '@tanstack/svelte-query' | ||
| const query = createQuery(() => ({ | ||
| queryKey: ['repoData'], | ||
| queryFn: () => | ||
| fetch('https://api.github.com/repos/tannerlinsley/react-query').then( | ||
| (res) => res.json(), | ||
| ), | ||
| })) | ||
| </script> | ||
| <!-- ❌ react version -- supports destructing outside reactive context | ||
| const { isPending, error, data } = useQuery({ | ||
Comment on lines
+83
to
+84
Contributor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win Correct “destructing” to “destructuring”. The comment names the JavaScript operation incorrectly. 🤖 Prompt for AI Agents | ||
| queryKey: ['repoData'], | ||
| queryFn: () => | ||
| fetch('https://api.github.com/repos/tannerlinsley/react-query').then( | ||
| (res) => res.json(), | ||
| ), | ||
| }) --> | ||
| <!-- ✅ access query properties in svelte reactive context --> | ||
| <div> | ||
| {#if query.isPending} | ||
| <p>Loading...</p> | ||
| {:else if query.isError} | ||
| <p>Error: {query.error.message}</p> | ||
| {:else if query.isSuccess} | ||
| <div> | ||
| <h1>{query.data.name}</h1> | ||
| <p>{query.data.description}</p> | ||
| <strong>👀 {query.data.subscribers_count}</strong> | ||
| <strong>✨ {query.data.stargazers_count}</strong> | ||
| <strong>🍴 {query.data.forks_count}</strong> | ||
| </div> | ||
| {/if} | ||
| </div> | ||
| ``` | ||
| - Runes values can be passed in directly to function arguments. Svelte Query will update the query automatically. | ||
| ```svelte | ||
| <script lang="ts"> | ||
| import { createQuery } from '@tanstack/svelte-query' | ||
| let enabled = $state(false); | ||
| let todoCount = $state(0); | ||
| // ✅ passing a rune directly is safe and observers update | ||
| // automatically when the value of a rune changes | ||
| const todosQuery = createQuery(() => ({ | ||
| queryKey: ['todos'], | ||
| queryFn: () => fetchTodos(), | ||
| enabled: enabled, | ||
| })) | ||
| const todoDetailsQuery = createQuery(() => ({ | ||
| queryKey: ['todo', todoCount], | ||
| queryFn: fetchTodo, | ||
| enabled: todoCount > 0, | ||
| })) | ||
| </script> | ||
| <div> | ||
| {#if todosQuery.isPending} | ||
| <p>Loading...</p> | ||
| {:else if todosQuery.isError} | ||
| <p>Error: {todosQuery.error.message}</p> | ||
| {:else if todosQuery.isSuccess} | ||
| {#each todosQuery.data as todo} | ||
| <button onClick={() => (todoCount = todo.id)}>{todo.title}</button> | ||
| {/each} | ||
| {/if} | ||
| <button onClick={() => (enabled = !enabled)}>Toggle enabled</button> | ||
Comment on lines
+141
to
+144
Contributor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🎯 Functional Correctness | 🟠 Major | ⚡ Quick win 🧩 Analysis chain🏁 Script executed: #!/usr/bin/env bashset -euo pipefail
rg -n '\bonClick=|\bonclick=' docs/framework/svelteRepository: TanStack/query Length of output: 379 🏁 Script executed: #!/usr/bin/env bashset -euo pipefail
printf'%s\n''--- quick-start context ---'
sed -n '120,150p' docs/framework/svelte/quick-start.md
printf'%s\n''--- Svelte versions and event-handler examples ---'
rg -n -i 'svelte(@|["'\'']|:)|onclick=|on:click|onClick=' \
package.json pnpm-lock.yaml docs/framework/svelte packages 2>/dev/null | head -200Repository: TanStack/query Length of output: 29198 🌐 Web query:
💡 Result: In Svelte 5, you must use lowercase attribute names for event handlers (e.g., onclick) [1][2][3]. Official documentation specifies that event attributes are case-sensitive [1]. While HTML attributes are often case-insensitive, Svelte 5 treats onclick and onClick as distinct; onclick is the standard attribute that correctly binds to the DOM click event [1]. Using an incorrect case, such as onClick, will not trigger the expected event handler [4][5][2]. This change is part of the Svelte 5 migration away from the on: directive in favor of using standard DOM event attributes as properties [6][7][3]. For example, instead of the Svelte 4 syntax on:click={handler}, you should use onclick={handler} [6][7]. Key takeaways regarding this change include: - Consistency: Event handler names must be fully lowercase [2][3]. - Case-sensitivity: Svelte distinguishes between different casings to allow for custom events that might use uppercase characters, meaning incorrect casing for standard DOM events will fail [1]. - Migration: When migrating, remember to remove the colon (:) and ensure the property name is all lowercase [6][7]. - Best practice: Always use lowercase standard DOM attribute names to ensure compatibility and avoid silent failures [2]. Citations:
Use lowercase Svelte event attributes. Replace both 🤖 Prompt for AI AgentsSource: MCP tools | ||
| </div> | ||
| ``` | ||
| - Errors can be caught and reset using Svelte's native `<svelte:boundary>` component. | ||
| Set `throwOnError` option to `true` to make sure errors are thrown to the `<svelte:boundary>` component. | ||
| - Since Property tracking is handled through Svelte's fine grained reactivity, options like `notifyOnChangeProps` are not needed | ||
Contributor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win Use the hyphenated compound adjective. Change 🧰 Tools🪛 LanguageTool[grammar] ~151-~151: Use a hyphen to join words. (QB_NEW_EN_HYPHEN) 🤖 Prompt for AI AgentsSource: Linters/SAST tools | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎯 Functional Correctness | 🟠 Major | ⚡ Quick win
🧩 Analysis chain
🏁 Script executed:
Repository: TanStack/query
Length of output: 3795
🏁 Script executed:
Repository: TanStack/query
Length of output: 21460
🏁 Script executed:
Repository: TanStack/query
Length of output: 35113
Describe the query result as a reactive object.
createQueryreturns aCreateQueryResult, which is a reactiveQueryObserverResult, not a store. Replace “store” to avoid incorrect$store syntax.🤖 Prompt for AI Agents
Source: MCP tools