Router
The imperative router contract.
Compatibility
Import from @kasperrt/smolter/react @kasperrt/smolter/preact
Signature
interface Router {
routes: readonly Route[];
getSnapshot: () => Snapshot;
subscribe: (listener: () => void) => () => void;
start: () => () => void;
navigate: (url: string, options?: NavigateOptions) => Promise<void>;
revalidate: (
options?: RevalidateOptions | RevalidationArgs["reason"],
context?: RevalidationContext,
) => Promise<void>;
/** Register layout data under a route ID. The returned function unregisters it. */
registerRevalidator: (id: string, revalidate: Revalidator) => () => void;
submit: (
url: string,
body: FormData,
options?: SubmitOptions,
) => Promise<Result>;
/** Create an independent fetcher. Framework useFetcher hooks manage disposal automatically. */
createFetcher: () => Fetcher;
/** Speculatively read route data without navigation; cached for one navigation, at most 30 seconds. */
prefetch: (url: string) => Promise<Result>;
dispose: () => void;
}Usage
The imperative router contract. start installs history and begins the first load; its return value releases that lifecycle registration. navigate changes routes. revalidate refreshes active route data, optionally targeting IDs or path patterns with { route }. registerRevalidator adds root/layout callbacks under an ID and returns an unregister function. subscribe observes immutable snapshots. dispose aborts all active channels.
const stop = router.start();
const unsubscribe = router.subscribe(() => console.log(router.getSnapshot()));
await router.navigate("/tasks");
unsubscribe();
stop();
router.dispose();Fields
routes
routes: readonly Route[];getSnapshot
getSnapshot: () => Snapshot;subscribe
subscribe: (listener: () => void) => () => void;start
start: () => () => void;revalidate
revalidate: (
options?: RevalidateOptions | RevalidationArgs["reason"],
context?: RevalidationContext,
) => Promise<void>;registerRevalidator
registerRevalidator: (id: string, revalidate: Revalidator) => () => void;Register layout data under a route ID. The returned function unregisters it.
submit
submit: (
url: string,
body: FormData,
options?: SubmitOptions,
) => Promise<Result>;createFetcher
createFetcher: () => Fetcher;Create an independent fetcher. Framework useFetcher hooks manage disposal automatically.
prefetch
prefetch: (url: string) => Promise<Result>;Speculatively read route data without navigation; cached for one navigation, at most 30 seconds.
dispose
dispose: () => void;