Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

queries.md

92 lines1.7 KB

<script lang="ts">
  import { createQuery } from '@tanstack/svelte-query'
  const todosQuery = createQuery(() => ({
    queryKey: ['todos'],
    queryFn: fetchTodoList,
  }))
</script>
const todosQuery = createQuery(() => ({
  queryKey: ['todos'],
  queryFn: fetchTodoList,
}))
<script lang="ts">
  import { createQuery } from '@tanstack/svelte-query'

  const todosQuery = createQuery(() => ({
    queryKey: ['todos'],
    queryFn: () => fetchTodos(),
  }))
</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}
      <p>{todo.title}</p>
    {/each}
  {/if}
</div>

If booleans aren't your thing, you can always use the status state as well:

<script lang="ts">
  const todosQuery = createQuery(() => ({
    queryKey: ['todos'],
    queryFn: () => fetchTodos(),
  }))
</script>

<div>
  {#if todosQuery.status === 'pending'}
    <p>Loading...</p>
  {:else if todosQuery.status === 'error'}
    <p>Error: {todosQuery.error.message}</p>
  {:else if todosQuery.status === 'success'}
    {#each todosQuery.data as todo}
      <p>{todo.title}</p>
    {/each}
  {/if}
</div>