Skip to content
These docs describe v4, which is in beta: install it with @beta. For v3, read the docs in the v3 branch.

React Router Example

Example of using React Router 8 in framework mode can be found in the examples/react-router-app directory of the repository. A route loader prefetches a query on the server, and the route component reads the same data through the generated hook.

React Router 8 needs Node.js 22.22 or later and React 19.2.7 or later. Import everything from react-router: react-router-dom has no v8.

{ "scripts": { "codegen": "orqc -i ./petstore.yaml" } }
fetchClient.ts
import { client } from "./openapi/requests/client.gen";
client.setConfig({
baseUrl: "http://localhost:4010",
throwOnError: true,
});
providers.tsx
import "./fetchClient";
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import React from "react";
export default function Providers({ children }: { children: React.ReactNode }) {
const [queryClient] = React.useState(
() =>
new QueryClient({
defaultOptions: {
queries: {
// Above 0, so the browser does not refetch what the server just sent.
staleTime: 30 * 1000,
},
},
}),
);
return (
<QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
);
}

Wrap <Outlet /> with it in app/root.tsx.

Call the generated prefetch function in the route's loader and return the dehydrated cache. The component hands it to HydrationBoundary:

app/routes/_index/route.tsx
import {
dehydrate,
HydrationBoundary,
QueryClient,
} from "@tanstack/react-query";
import { useFindPets } from "../../../openapi/queries";
import { prefetchUseFindPets } from "../../../openapi/queries/prefetch";
import type { Route } from "./+types/route";
export async function loader(_: Route.LoaderArgs) {
const queryClient = new QueryClient();
await prefetchUseFindPets(queryClient, {
query: { tags: [], limit: 10 },
});
return { dehydratedState: dehydrate(queryClient) };
}
function Pets() {
const { data } = useFindPets({ query: { tags: [], limit: 10 } });
return (
<ul>
{data?.map((pet) => (
<li key={pet.id}>{pet.name}</li>
))}
</ul>
);
}
export default function Index({ loaderData }: Route.ComponentProps) {
return (
<HydrationBoundary state={loaderData.dehydratedState}>
<Pets />
</HydrationBoundary>
);
}

Pass the hook the same options as the prefetch function, and no query key of your own as its second argument. The query key is the operation's key followed by the options, and a key of your own takes the place of the options. Either difference is a different cache entry, and the browser fetches again.

import { useQueryClient } from "@tanstack/react-query";
import { UseFindPetsKeyFn, useAddPet } from "../../../openapi/queries";
function AddPet({ name }: { name: string }) {
const queryClient = useQueryClient();
const { mutate: addPet } = useAddPet();
return (
<button
type="button"
onClick={() =>
addPet(
{ body: { name } },
{
onSuccess: () =>
queryClient.invalidateQueries({
queryKey: UseFindPetsKeyFn({ query: { tags: [], limit: 10 } }),
}),
},
)
}
>
Create a pet
</button>
);
}

From the root of the repository:

Terminal window
pnpm install
pnpm build
pnpm --filter @7nohe/react-router-app generate:api
pnpm --filter @7nohe/react-router-app dev

dev starts React Router and a small mock Petstore server on http://localhost:4010.