1.19.3 • Published 28 days ago

@generouted/react-router v1.19.3

Weekly downloads
-
License
MIT
Repository
github
Last release
28 days ago

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-dom

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-dom'

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-dom'

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

1.19.3

28 days ago

1.19.2

29 days ago

1.19.0

30 days ago

1.19.1

30 days ago

1.18.8

1 month ago

1.18.7

1 month ago

1.18.6

1 month ago

1.18.5

2 months ago

1.18.4

2 months ago

1.18.3

2 months ago

1.18.1

4 months ago

1.18.0

4 months ago

1.18.2

4 months ago

1.17.1

5 months ago

1.17.0

5 months ago

1.14.0

11 months ago

1.16.1

7 months ago

1.16.0

7 months ago

1.15.0

10 months ago

1.15.4

9 months ago

1.15.3

9 months ago

1.15.2

10 months ago

1.15.1

10 months ago

1.15.8

8 months ago

1.15.7

9 months ago

1.15.6

9 months ago

1.15.5

9 months ago

1.13.7

11 months ago

1.15.9

7 months ago

1.13.2

11 months ago

1.13.6

11 months ago

1.13.5

11 months ago

1.13.4

11 months ago

1.13.3

11 months ago

1.13.1

11 months ago

1.13.0

11 months ago

1.12.7

1 year ago

1.12.9

12 months ago

1.12.8

12 months ago

1.12.3

1 year ago

1.11.4

1 year ago

1.12.2

1 year ago

1.11.3

1 year ago

1.12.1

1 year ago

1.11.2

1 year ago

1.12.0

1 year ago

1.11.1

1 year ago

1.12.6

1 year ago

1.11.7

1 year ago

1.12.5

1 year ago

1.11.6

1 year ago

1.12.4

1 year ago

1.11.5

1 year ago

1.9.0

1 year ago

1.8.0

1 year ago

1.11.0

1 year ago

1.10.0

1 year ago

1.7.20

1 year ago

1.7.17

1 year ago

1.7.18

1 year ago

1.7.19

1 year ago

1.7.9

1 year ago

1.7.10

1 year ago

1.7.8

1 year ago

1.7.11

1 year ago

1.7.7

1 year ago

1.7.12

1 year ago

1.7.13

1 year ago

1.7.14

1 year ago

1.7.15

1 year ago

1.7.16

1 year ago

1.7.6

1 year ago

1.7.5

1 year ago

1.7.4

1 year ago

1.7.3

1 year ago

1.7.2

1 year ago

1.7.1

1 year ago

1.7.0

1 year ago