Skip to content

createRoute

Creates new route. Has required parameter path

Basic example

ts
import { createRoute } from '@argon-router/core';

const route = createRoute({ path: '/route' });

route.open();

In-route path params

Argon-router uses its own tool for parsing and building paths, while validating parameters for compliance with the specified type and other passed parameters constraints at runtime.

By default, each parameter is treated as a string:

ts
import { createRoute } from '@argon-router/core';

const postRoute = createRoute({ path: '/post/:id' });
//       ^- Route<{ id: string }>

However, you can also explicitly specify that a parameter should be of type number or union:

ts
import { createRoute } from '@argon-router/core';

const postRoute = createRoute({ path: '/post/:id<number>' });
//       ^- Route<{ id: number }>

const buildPostRoute = createRoute({ path: '/build-post/:mode<create|update>' });
//       ^- Route<{ mode: 'create' | 'update' }>

You can also specify a number of modifiers for each parameter. More details about the syntax can be found here.

Parent paths

Sometimes you want to nest path parts and beforeOpen effects from some routes. You can do this easily with parent routes:

ts
import { createRoute } from '@argon-router/core';

const profile = createRoute({ path: '/profile/:id', beforeOpen: [] });

const friends = createRoute({ path: '/friends', parent: profile });
const posts = createRoute({ path: '/posts', parent: profile });

posts.open(); // profile.$isOpened -> true, posts.$isOpened -> true

API

nametypedescription
$paramsStore<T>route path parameters
$isOpenedStore<boolean>is route opened (note: route opened if his child opened too)
$isPendingStore<boolean>is route open pending (if before open in progress)
openEventCallable<RouteOpenedPayload<T>>open route and it's parents
openedOnServerEvent<RouteOpenedPayload<T>>route opened on server (SSR)
openedOnClientEvent<RouteOpenedPayload<T>>route opened on client
openedEvent<RouteOpenedPayload<T>>route opened on server (SSR) or client
closedEvent<void>route closed
pathstringroute path
parentRoute<any>parent path
beforeOpenEffect<any, any>[]before open effects

Released under the MIT License.