smolter
API

defineRoute

Bind a path to a route module while retaining its exact handler types.

Compatibility

ReactAvailable in this renderer's browser integration.
React SSRAvailable to this renderer's SSR integration; browser event handlers run after hydration.
PreactAvailable in this renderer's browser integration.
Preact SSRAvailable to this renderer's SSR integration; browser event handlers run after hydration.

Import from @kasperrt/smolter/react @kasperrt/smolter/preact @kasperrt/smolter/utils

Signature

function defineRoute<const M extends RouteModule>(
  path: string,
  module: M,
): Route<M>;

Parameters

path is a URL pattern. module contains handlers, view exports, and an optional shouldRevalidate function.

Returns

A new route object containing the module exports and path.

Usage

Import this API from one of the entry points listed above. Keep server and build helpers in their dedicated subpaths.

import * as task from "./routes/task/route";
const taskRoute = defineRoute("/tasks/:id", task);
const data = useLoaderData(taskRoute);

Behavior and edge cases

The supplied path wins over any path field on the module. This helper does not register a route globally; include the result in the table passed to createDataRouter.

View implementation ↗