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

The @rspc/react-query adapter wraps TanStack Query v5. This example uses the tuple client and ProceduresLegacy bindings from the quickstart.

To get started first install the required packages.

npm install @rspc/client@0.3.1 @rspc/react-query@0.3.1 @tanstack/react-query@5

Then you can do the following:

index.ts
import { QueryClient } from "@tanstack/react-query";
import { FetchTransport, createClient } from "@rspc/client";
import { createReactQueryHooks } from "@rspc/react-query";

import type { ProceduresLegacy as Procedures } from "./bindings";

// You must provide the generated types as a generic and create a transport (in this example we are using HTTP Fetch) so that the client knows how to communicate with your API.
const client = createClient<Procedures>({
  // Refer to the integration your using for the correct transport.
  transport: new FetchTransport("/rspc"),
});

const queryClient = new QueryClient();
const rspc = createReactQueryHooks<Procedures>();

function SomeComponent() {
  const { data, isLoading, error } = rspc.useQuery(["version", null]);
  const greeting = rspc.useMutation("greet");

  return (
    <>
      <p>{data}</p>
      <button onClick={() => greeting.mutate("Ada")}>Greet</button>
    </>
  );
}

function App() {
  return (
    <rspc.Provider client={client} queryClient={queryClient}>
      <SomeComponent />
    </rspc.Provider>
  );
}