> For AI agents: the complete documentation index is available at /next/llms.txt, the full documentation bundle is available at /next/llms-full.txt.

# TanStack Router

Modern and scalable routing for React applications.

ReactLynx supports both [Memory Routing](https://tanstack.com/router/latest/docs/framework/react/guide/history-types#memory-routing)
and [File-Based Routing](https://tanstack.com/router/latest/docs/framework/react/routing/file-based-routing),
where Memory Routing is required due to browser [History API](https://developer.mozilla.org/en-US/docs/Web/API/History_API) limitations in Lynx,
and File-Based Routing is enabled through seamless `@tanstack/router-plugin/rspack` integration in Rspeedy.

### Installation

Install dependencies:


```sh [npm]
npm install @tanstack/react-router url-search-params-polyfill
```

```sh [yarn]
yarn add @tanstack/react-router url-search-params-polyfill
```

```sh [pnpm]
pnpm add @tanstack/react-router url-search-params-polyfill
```

```sh [bun]
bun add @tanstack/react-router url-search-params-polyfill
```

```sh [deno]
deno add npm:@tanstack/react-router npm:url-search-params-polyfill
```

Install devDependencies:


```sh [npm]
npm install @tanstack/router-plugin -D
```

```sh [yarn]
yarn add @tanstack/router-plugin -D
```

```sh [pnpm]
pnpm add @tanstack/router-plugin -D
```

```sh [bun]
bun add @tanstack/router-plugin -D
```

```sh [deno]
deno add npm:@tanstack/router-plugin -D
```

### Project Configuration

- **File-based Routing**: Enabled via `@tanstack/router-plugin/rspack` for seamless routing integration.
- **React 18 API Compatibility**: Use `@lynx-js/react/compat` to ensure third-party libraries work correctly.

> **❓ Why use @lynx-js/react/compat**
> 
> ReactLynx offers React 18 API compatibility via `@lynx-js/react/compat`,
> providing crucial APIs such as `React.startTransition` required by TanStack
> Router.
> 


```js title=lynx.config.js
import { defineConfig } from '@lynx-js/rspeedy';
import { tanstackRouter } from '@tanstack/router-plugin/rspack';
import { createRequire } from 'node:module';

const require = createRequire(import.meta.url);

export default defineConfig({
  // ...other configs
  source: {
    alias: {
      react$: require.resolve('@lynx-js/react/compat'),
    },
  },
  tools: {
    rspack: {
      plugins: [
        tanstackRouter({
          target: 'react',
        }),
      ],
    },
  },
});
```

### Router Configuration

In the Lynx environment, the [document](https://developer.mozilla.org/en-US/docs/Web/API/Document) is `undefined`, causing the router to incorrectly identify it as `isServer: true`. You need to explicitly set `isServer: false` to ensure proper rendering.

Additionally, [Memory Routing](https://tanstack.com/router/latest/docs/framework/react/guide/history-types#memory-routing) is required due to browser [History API](https://developer.mozilla.org/en-US/docs/Web/API/History_API) limitations in Lynx.

```tsx title=App.tsx
import {
  createMemoryHistory,
  createRouter,
  RouterProvider,
} from '@tanstack/react-router';

import { routeTree } from './routeTree.gen.js';

const memoryHistory = createMemoryHistory({
  initialEntries: ['/'],
});

const router = createRouter({
  routeTree,
  history: memoryHistory,
  isServer: false, // [!code highlight]
});

export function App() {
  return <RouterProvider router={router} />;
}
```

### Polyfill

To ensure [URLSearchParams API](https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams) works correctly in the Lynx environment,
add `url-search-params-polyfill` at the top of your entry file:

```tsx title=index.tsx
import 'url-search-params-polyfill';

import { root } from '@lynx-js/react';
import { App } from './App.jsx';

root.render(<App />);
```

### File-Based

TanStack Router supports file-based routing where your file structure defines your routes.
Place your route components in the `src/routes` directory, with `__root.tsx` serving as the root component.

```jsx title="src/routes/__root.tsx"
import { createRootRoute, Outlet } from '@tanstack/react-router';

export const Route = createRootRoute({
  component: () => (
    <view>
      <Outlet />
    </view>
  ),
});
```

```jsx title="src/routes/Index.tsx"
import { createFileRoute } from '@tanstack/react-router';

export const Route = createFileRoute('/')({
  component: () => <view>Index Page</view>,
});
```

The `@tanstack/router-plugin/rspack` automatically scans the `src/routes` directory and generates a `routeTree.gen.ts` file
that contains all the route definitions and type information. This generated file includes:

- Route imports and configurations
- TypeScript interfaces for type-safe routing
- The complete route tree structure

See [TanStack File-Based Routing](https://tanstack.com/router/latest/docs/framework/react/routing/file-based-routing) for more details.

### Example

**This is an example below: tanstack-router**

**Entry:** `src/tanstack-router`
**Bundle:** `dist/main.lynx.bundle` | Web: `dist/main.web.bundle`

```tsx
import { createMemoryHistory, createRouter, RouterProvider } from "@tanstack/react-router";

import { routeTree } from "./routeTree.gen.js";

const memoryHistory = createMemoryHistory({
  initialEntries: ["/"],
});

const router = createRouter({
  routeTree,
  history: memoryHistory,
  isServer: false,
  origin: "http://localhost",
});

export function App() {
  return <RouterProvider router={router} />;
}

```

v0.7.0


