You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
-**Router params are strings**: TanStack Router params are always strings. You must parse them to the correct type (e.g., `Number(params.petId)`) before passing to the loader.
73
-
-**External cache configuration**: When using TanStack Query as the cache, set `defaultPreloadStaleTime: 0` in your router configuration to let React Query handle cache freshness:
74
-
75
-
```tsx
76
-
const router =createRouter({
77
-
routeTree,
78
-
defaultPreloadStaleTime: 0,
79
-
});
80
-
```
81
-
82
-
-**Link preloading**: When using `<Link preload="intent">` or `defaultPreload: "intent"`, TanStack Router will automatically call the route's `loader` on hover/touch. If your loader uses `ensureUse*Data`, prefetching happens automatically without needing `withQueryPrefetch`.
Copy file name to clipboardExpand all lines: docs/src/content/docs/examples/tanstack-router.md
+25-51Lines changed: 25 additions & 51 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -5,27 +5,21 @@ description: Using TanStack Router with OpenAPI React Query Codegen for data loa
5
5
6
6
Example of using TanStack Router can be found in the [`examples/tanstack-router-app`](https://github.com/7nohe/openapi-react-query-codegen/tree/main/examples/tanstack-router-app) directory of the repository.
7
7
8
-
## Generated Files
8
+
## Route Data Loading
9
9
10
-
The codegen generates a `router.ts` file that provides:
11
-
12
-
-**Loader factories** (`loaderUse*`) for route data loading
13
-
-**`withQueryPrefetch`** helper for hover/touch prefetching
14
-
15
-
## Using Loader Factories
16
-
17
-
Use `loaderUse*` functions in your route definitions to prefetch data before the route renders:
10
+
Use the generated `ensureQueryData` functions in your route loaders to prefetch data before the route renders:
You can pass additional client options through the `clientOptions` parameter:
62
+
Use `prefetchQuery` functions for custom prefetch triggers:
71
63
72
64
```tsx
73
-
loader: ({ params }) =>
74
-
loaderUseFindPetById({
75
-
queryClient,
76
-
clientOptions: {
77
-
headers: { "X-Custom-Header": "value" },
78
-
},
79
-
})({
80
-
params: { petId: Number(params.petId) },
81
-
}),
82
-
```
83
-
84
-
## Using withQueryPrefetch
85
-
86
-
The `withQueryPrefetch` helper enables prefetching on hover or touch events. This is useful for custom prefetch triggers outside of TanStack Router's built-in `<Link>` preloading:
When using `preload="intent"`, the router automatically calls the route's `loader` on hover/touch. If your loader uses `ensureUse*Data` (which the generated loaders do), prefetching happens automatically.
121
+
When using `preload="intent"`, the router automatically calls the route's `loader` on hover/touch.
146
122
147
123
## Important Notes
148
124
149
125
### Router Params Are Strings
150
126
151
-
TanStack Router params are always strings. You must parse them to the correct type before passing to the loader:
127
+
TanStack Router params are always strings. You must parse them to the correct type:
152
128
153
129
```tsx
154
-
// Router params: { petId: string }
155
-
// API expects: { petId: number }
156
130
loader: ({ params }) =>
157
-
loaderUseFindPetById({ queryClient })({
158
-
params: { petId: Number(params.petId) }, // Convert string to number
131
+
ensureUseFindPetByIdData(queryClient, {
132
+
path: { petId: Number(params.petId) }, // Convert string to number
Generates custom functions that use React Query's `ensureQueryData` and `prefetchQuery` functions, plus loader factories for TanStack Router, to integrate into frameworks like Next.js, Remix, and TanStack Start.
27
+
Generates custom functions that use React Query's `ensureQueryData` and `prefetchQuery` functionsto integrate into frameworks like Next.js, Remix, and TanStack Router.
Generates pure TypeScript clients generated by [@hey-api/openapi-ts](https://github.com/hey-api/openapi-ts) in case you still want to do type-safe API calls without React Query.
0 commit comments