TanStack Router

Modern and scalable routing for React applications.

ReactLynx supports both Memory Routing and File-Based Routing, where Memory Routing is required due to browser History API limitations in Lynx, and File-Based Routing is enabled through seamless @tanstack/router-plugin/rspack integration in Rspeedy.

Installation

Install dependencies:

npm
yarn
pnpm
bun
npm install @tanstack/react-router url-search-params-polyfill

Install devDependencies:

npm
yarn
pnpm
bun
npm install @tanstack/router-plugin -D

Config

  • 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.

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',
        }),
      ],
    },
  },
});

Polyfill

To ensure URLSearchParams API works correctly in the Lynx environment, add url-search-params-polyfill at the top of your entry file:

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.

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

export const Route = createRootRoute({
  component: () => (
    <view>
      <Outlet />
    </view>
  ),
});
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 for more details.

Example

Except as otherwise noted, this work is licensed under a Creative Commons Attribution 4.0 International License, and code samples are licensed under the Apache License 2.0.