# @monarcode/effetch-query/react/tanstack-router (https://effetch-query.monarcode.dev/docs/reference/tanstack-router)

`@monarcode/effetch-query/react/tanstack-router` connects a query client to a TanStack Router, and so to TanStack Start. Import what you use:

```ts
import { connectQueryClient } from "@monarcode/effetch-query/react/tanstack-router";
```

Its exports follow, grouped by the guide that covers them, each with its
docs. Your editor shows the same docs, with an example, when you hover the
name.

## TanStack Router and Start

Guide: [TanStack Router and Start](https://effetch-query.monarcode.dev/docs/frameworks/tanstack-start).

### connectQueryClient

Connects a query client to a TanStack Router (and so TanStack Start), as TanStack's `setupRouterSsrQueryIntegration` does for TanStack Query. Call it where the router is made: Start's `getRouter`, or your server and browser entries.

- **On the server**, the queries your loaders loaded go with the page, and queries started while rendering follow as they start, those still loading as promises the browser waits for, so nothing is fetched twice. The client is disposed when the request ends: make one per request from a process-wide `ManagedRuntime` (`QueryClient.make({ runtime })`), so your app's layer is built once rather than per request (see the example).
- **In the browser**, they load into the client before React hydrates, with no `HydrationBoundary`.
- **Redirects:** a query or mutation that fails or dies with a router `redirect()` navigates there, in the browser. Write it as `Effect.die(redirect({ to: "/login" }))`, to keep it out of the error type, inside the Effect so each run makes its own: the router resolves a redirect in place. A fetch an Effect awaits (`Query.fetch` or `Query.prefetch`) is left to it: in a loader, `client.fetch` rejects with the redirect and the router follows it, and a preload's is dropped, as the router drops it. So load a query that may redirect with `client.fetch`: one only prefetched fails in its readers instead.

Load data in loaders with `context.queryClient.fetch(query)`, which returns a fresh value without fetching, and set the router's `defaultPreloadStaleTime` to `0`, so the queries' `staleTime` decides what's refetched rather than the router's own loader cache. Only queries with a `schema` cross to the browser. A `dehydrate.queries` key that holds a query, a family or a group fails each server render with a `Query.KeyHoldsTargetError`.

### ConnectDehydrateOptions

Which queries [`connectQueryClient`](https://effetch-query.monarcode.dev/docs/reference/tanstack-router#connectqueryclient) sends from the server, as [`QueryClient.DehydrateOptions`](https://effetch-query.monarcode.dev/docs/reference/query-client#dehydrateoptions) chooses them. Queries still loading are always sent, as promises the browser waits for: the router's serializer carries them.

| Prop        | Type    | Description                                                                                                                                                               |
| ----------- | ------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `queries?`  | `union` | A target: only the queries it matches are sent, as Query.invalidate matches them. Default: every query.                                                                   |
| `failures?` | `union` | A failed query: `"include"` sends it with its error, encoded with the query's `schema.error`; `"omit"` leaves it out, so the browser fetches it again. Default: `"omit"`. |

### ConnectQueryClientOptions

Options for [`connectQueryClient`](https://effetch-query.monarcode.dev/docs/reference/tanstack-router#connectqueryclient).

| Prop               | Type        | Description                                                                                                                                                                          |
| ------------------ | ----------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| `router`           | `AnyRouter` | The router, made for this request on the server.                                                                                                                                     |
| `queryClient`      | `object`    | The query client, made with the router and connected to no other. On the server it must be this request's own: it's disposed when the request ends.                                  |
| `dehydrate?`       | `object`    | The queries the server sends. Default: every query that can be sent (one with a `schema`, or an Effetch endpoint's with an `output`), failures left out.                             |
| `handleRedirects?` | `union`     | If true, a query or mutation that fails or dies with a `redirect()` navigates there, in the browser, unless an Effect awaits the fetch (a loader's `client.fetch`). Default: `true`. |
| `wrapQueryClient?` | `union`     | If true, wraps the router's `Wrap` in a `QueryProvider` for the client; if false, render your own. Default: `true`.                                                                  |