rspc is no longer maintained. These docs are kept for reference.

Svelte

Use the typed client as a Svelte query function.

Use the vanilla client as a TanStack Query request function. This avoids depending on the experimental rspc Svelte adapter's context setup.

The example below uses the Svelte 4 store-based API with TanStack Svelte Query 5.71.10. For a newer adapter generation, follow its setup guide and keep the typed client.query call as the query function.

npm install @rspc/client@0.3.1 @tanstack/svelte-query@5.71.10

Provide a query client

src/App.svelte
<script lang="ts">
  import { QueryClient, QueryClientProvider } from "@tanstack/svelte-query";
  import Version from "./Version.svelte";

  const queryClient = new QueryClient();
</script>

<QueryClientProvider client={queryClient}>
  <Version />
</QueryClientProvider>

Call a query

Create api.ts using the quickstart client setup, then use it in a child component:

src/Version.svelte
<script lang="ts">
  import { createQuery } from "@tanstack/svelte-query";
  import { client } from "./api";

  const version = createQuery({
    queryKey: ["version"],
    queryFn: () => client.query(["version", null]),
  });
</script>

{#if $version.isPending}
  <p>Loading...</p>
{:else if $version.isError}
  <p>Could not load version.</p>
{:else}
  <p>{$version.data}</p>
{/if}

For server-side rendering, create query clients per request and use a server-reachable API URL. Do not share a cache containing user-specific data across requests.