PageRoutes component and its sub-components are used to define routing for app pages. Use createPageRouter to wrap your route definitions, which returns a React component that handles routing.
Basic example
Here’s a complete example showing how to create multiple pages in your app with routing:createPageRouter
ThecreatePageRouter function accepts JSX route definitions and returns a React component that handles routing. Call it at the module level with your <PageRoutes> tree:
Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
routes | ReactNode | Yes | A <PageRoutes> JSX tree defining all routes for your app. |
Returns
A React component (PageRouter) that you render in your app to activate routing.
PageRoutes
ThePageRoutes component is the container for route definitions. When used as the root element passed to createPageRouter, it defines the top-level routes. When nested inside another <PageRoutes>, it defines a group of sub-routes under a shared path prefix.
Props
PageRoutes.IndexRoute
ThePageRoutes.IndexRoute component defines the home page (index route) of your app or a nested section. This is the page that will be rendered when users navigate to the base URL of the enclosing PageRoutes.
Props
PageRoutes.Route
ThePageRoutes.Route component defines a named page route in your app. Users can navigate to these routes using the path specified in the path prop.
Props
PageRoutes.AnyRoute
ThePageRoutes.AnyRoute component defines a catch-all route that will be rendered when no other routes match. This is useful for displaying a custom 404 or “page not found” message.
Props
Guidelines
- DO: use
createPageRouterto define your routes and render the returned component. - DO: use
PageRoutes.IndexRouteto define your app’s home page. - DO: use meaningful path names that describe the page content (e.g., “/docs”, “/settings”, “/support”).
- DO: include a
PageRoutes.AnyRouteto handle unmatched routes gracefully. - DO: use
layoutComponentto share common UI across groups of routes. - DO: assign an
idto each route when you need to identify the active route in layout components.