defineRoute
Builds a typed route.
Builds a typed route. The path is checked when the route is defined (an invalid pattern throws
here, not at the first navigation); the options' callbacks are typed by the path, so params in
beforeLoad and loader is { id: string } for /posts/:id.
Import#
ts
import { defineRoute } from "@shadow-js/router";
Signature#
ts
function defineRoute<P extends string, S = SearchRecord, L = undefined>(
path: P,
options: RouteOptions<P, S, L> = {},
): RouteDefinition<P, L, S>
Parameters#
| Name | Description |
|---|---|
path |
Route path, relative to the parent ("" for a pathless layout, which then needs an explicit id). |
options |
Components, search validation, loaders and children. |
Returns#
The route, to pass to createRouter or to another route's children.
Throws#
- TypeError for a pathless route without an id.
- PatternSyntaxError (from
@shadow-js/router/core) for an invalid pattern.
Examples#
ts
const post = defineRoute("/posts/:id", {
loader: ({ params }) => fetchPost(params.id),
component: () => <PostPage />,
});