Generated file-based routes for React Router and Vite
npm install @generouted/react-routerCheck out generouted's main docs for the features, conventions and more.
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/*.
In case you don't have a Vite project with React and TypeScript, check Vite documentation to start a new project.
``shell`
pnpm add @generouted/react-router react-router
`ts
// 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()] })
`
`tsx
// 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(
`
Add the home page by creating a new file src/pages/index.tsx → /, then export a default component:
`tsx
// src/pages/index.tsx
export default function Home() {
return
$3
`tsx
// src/pages/_app.tsximport { Outlet } from 'react-router'
export default function App() {
return (
)
}
`$3
Autocompletion for
Link, useNavigate, useParams and more exported from src/router.ts`tsx
// 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 (
{/* ✅ Passes /}
{/* 🔴 Error: not defined route /}
{/* 🔴 Error: missing required params /}
Home
)
}
`$3
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:`tsx
// src/pages/+login.tsximport { Modal } from '@/ui'
export default function Login() {
return Content
}
`To navigate to a modal use
useModals hook exported from src/router.ts:`tsx
// src/pages/_app.tsximport { Outlet } from 'react-router'
import { useModals } from '../router'
export default function App() {
const modals = useModals()
return (
)
}
`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 })`- Plugin
MIT