Key Features
- Type-safe routing - Routes are defined as enums, catching invalid routes at compile time
- Nested routes - Support for layouts and nested route structures
- URL parameters - Extract dynamic segments, query parameters, and hash fragments
- Declarative navigation - Use the
Linkcomponent or programmatic navigation - Cross-platform - Works on web, desktop, mobile, and server-side rendering
Basic Setup
To use routing in your application, you need three things:- Define a
Routeenum with the#[derive(Routable)]macro - Add a
Routercomponent to your app - Create component functions for each route
How It Works
The#[derive(Routable)] macro generates code that:
- Parses URLs into your
Routeenum variants - Converts
Routevariants back into URLs - Renders the appropriate component for each route
- Extracts dynamic segments into component props
- Parses the new URL path
- Matches it against your route definitions
- Extracts any parameters from the URL
- Renders the corresponding component with those parameters
Route Matching
Routes are matched in the order they appear in your enum. The router tries each variant from top to bottom and uses the first match:Place more specific routes before catch-all routes to ensure they match correctly.
Route Components
By default, the router looks for a component with the same name as your route variant:Navigation
There are two ways to navigate between routes:Using Link Component
Programmatic Navigation
Router Component Props
TheRouter component accepts optional configuration:
Cross-Platform Support
The router automatically adapts to different platforms:- Web: Uses browser history API and URL bar
- Desktop: Uses in-memory history (no visible URL bar)
- Mobile: Uses in-memory history
- SSR: Supports server-side rendering and hydration
Next Steps
- Defining Routes - Learn all route attribute options
- Navigation - Master the Link component and programmatic navigation
- Nested Routes - Build complex layouts with nested routing
- Route Parameters - Extract data from URLs