This example very briefly illustrates the 3 core concepts of Svelte Query:
<script>//App.svelteimport { QueryClient, QueryClientProvider } from '@sveltestack/svelte-query'import Todos from 'Todos.svelte'// Create a clientconst queryClient = new QueryClient()</script><QueryClientProvider client={queryClient}><Todos /></QueryClientProvider>
<script>// Todos.svelteimport { useQuery, useMutation, useQueryClient } from '@sveltestack/svelte-query'import { getTodos, postTodo } from '../my-api'// Access the clientconst queryClient = useQueryClient()// Queriesconst queryResult = useQuery('todos', getTodos)// Mutationsconst mutation = useMutation(postTodo, {onSuccess: () => {// Invalidate and refetchqueryClient.invalidateQueries('todos')},})</script><div><ul>{#each $queryResult.data as todo}<li>{todo.title}</li>{/each}</ul><buttonon:click={() => {$mutation.mutate({ id: Date.now(), title: 'Do Laundry' })}}>Add Todo</button></div>
These three concepts make up most of the core functionality of Svelte Query. The next sections of the documentation will go over each of these core concepts in great detail.
The latest TanStack news, articles, and resources, sent to your inbox.