@generouted/react-router

中文文档
1.20.2发布于 2026/03/15MITTypeScriptESM + CJS近 30 天下载 28.1万

Generated file-based routes for React Router and Vite

原始使用文档
文档目录13 节
  1. Docs
  2. How
  3. Getting started
  4. Installation
  5. Setup
  6. Usage
  7. Adding pages
  8. Optional root layout at pages/_app.tsx
  9. Type-safe navigation
  10. Type-safe global modals
  11. Examples
  12. React Router
  13. License

Generouted + React Router + Type-safety

Docs

Check out generouted's main docs for the features, conventions and more.

How

This integration is based on a Vite plugin to generate routes types for React Router with generouted conventions. The output is saved by default at src/router.ts and gets updated by the add/change/delete at src/pages/*.

Getting started

In case you don't have a Vite project with React and TypeScript, check Vite documentation to start a new project.

Installation

pnpm add @generouted/react-router react-router

Setup

// vite.config.ts

import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
import generouted from '@generouted/react-router/plugin'

export default defineConfig({ plugins: [react(), generouted()] })

Usage

// src/main.tsx

import { createRoot } from 'react-dom/client'
import { Routes } from '@generouted/react-router'
// import { Routes } from '@generouted/react-router/lazy' // route-based code-splitting

createRoot(document.getElementById('root')!).render(<Routes />)

Adding pages

Add the home page by creating a new file src/pages/index.tsx → /, then export a default component:

// src/pages/index.tsx

export default function Home() {
  return <h1>Home</h1>
}

Optional root layout at pages/_app.tsx

// src/pages/_app.tsx

import { Outlet } from 'react-router'

export default function App() {
  return (
    <section>
      <header>
        <nav>...</nav>
      </header>

      <main>
        <Outlet />
      </main>
    </section>
  )
}

Type-safe navigation

Autocompletion for Link, useNavigate, useParams and more exported from src/router.ts

// src/pages/index.tsx
import { Link, useNavigate, useParams } from '../router'

export default function Home() {
  const navigate = useNavigate()

  // typeof params -> { id: string; pid?: string }
  const params = useParams('/posts/:id/:pid?')

  // typeof params to be passed -> { id: string; pid?: string }
  const handler = () => navigate('/posts/:id/:pid?', { params: { id: '1', pid: '0' } })

  return (
    <div>
      {/** ✅ Passes  */}
      <Link to="/" />
      <Link to="/posts/:id" params={{ id: '1' }} />
      <Link to="/posts/:id/:pid?" params={{ id: '1' }} />
      <Link to="/posts/:id/:pid?" params={{ id: '1', pid: 0 }} />

      {/** 🔴 Error: not defined route  */}
      <Link to="/not-defined-route" />

      {/** 🔴 Error: missing required params */}
      <Link to="/posts/:id" />

      <h1>Home</h1>
    </div>
  )
}

Type-safe global modals

Although all modals are global, it's nice to co-locate modals with relevant routes.

Create modal routes by prefixing a valid route file name with a plus sign +. Why +? You can think of it as an extra route, as the modal overlays the current route:

// src/pages/+login.tsx

import { Modal } from '@/ui'

export default function Login() {
  return <Modal>Content</Modal>
}

To navigate to a modal use useModals hook exported from src/router.ts:

// src/pages/_app.tsx

import { Outlet } from 'react-router'

import { useModals } from '../router'

export default function App() {
  const modals = useModals()

  return (
    <section>
      <header>
        <nav>...</nav>
        <button onClick={() => modals.open('/login')}>Open modal</button>
      </header>

      <main>
        <Outlet />
      </main>
    </section>
  )
}

With useModals you can use modals.open('/modal-path') and modals.close(), and by default it opens/closes the modal on the current active route.

Both methods come with React Router's navigate() options with one prop added at, for optionally navigating to a route while opening/closing a modal, and it's also type-safe!

  • modals.open(path, options)
  • modals.close(options)

at should be also a valid route path, here are some usage examples:

  • modals.open('/login', { at: '/auth', replace: true })
  • modals.open('/info', { at: '/invoice/:id', params: { id: 'xyz' } })
  • modals.close({ at: '/', replace: false })

Examples

React Router

License

MIT

版本与发布记录

@generouted/react-router 共发布 89 个版本,首次发布于 2023/01/20;过去 12 个月发布了 2 个版本。

版本发布日期距今
1.20.2latest2026/03/158 个月前
1.20.12026/03/148 个月前
1.20.02025/02/081.8 年前
1.19.112025/02/071.8 年前
1.19.102025/01/121.8 年前
1.19.92024/11/052 年前

关于 @generouted/react-router 的常见问题

@generouted/react-router 是什么?

Generated file-based routes for React Router and Vite

如何安装 @generouted/react-router?

在项目目录运行 npm install @generouted/react-router,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:react(>=18)、react-router(>=7)、vite(>=5)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@generouted/react-router 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@generouted/react-router 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@generouted/react-router 可以商用吗?

@generouted/react-router 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@generouted/react-router 的下载量有多少?

根据 npm 官方下载统计,@generouted/react-router 在 2026/10/16 – 2026/11/14 这 30 天内被下载 280,964 次,日均约 9,365 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@generouted/react-router 还在维护吗?

最新版本 1.20.2 发布于 2026/03/15(8 个月前);至今共发布 89 个版本,其中过去 12 个月发布了 2 个;首次发布于 2023/01/20。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。