Function: createRouter()
API / @xmachines/play-dom-router / createRouter
function createRouter(options): VanillaRouter;Defined in: play-dom-router/src/create-router.ts:79
Creates the vanilla router for a framework-agnostic routing.
Architecture:
- The router wraps a history and a routeTree, and it does nothing more
- It does NOT hold the routeMap, because a provider needs the routeMap as a prop
- It manages the history only, and it knows no framework
The reason for no routeMap:
- The router knows nothing about a state ID, because a state ID belongs to Play
- The RouteMap is the bridge between the router and the actor
- The provider receives the routeMap as a prop, and it resolves a path to a state ID
Use, parallel to the TanStack mode:
// Both modes: the same setupconst routeTree = extractMachineRoutes(machine);const routeMap = createRouteMapFromTree(routeTree);const history = createBrowserHistory({ window });
// The vanilla routerconst router = createRouter({ routeTree, history });
// Connect the router to the actor. This does all the work in both directionsconst disconnect = connectRouter({ actor, router, routeMap });
// Later: clean updisconnect();router.destroy();Or let a scope do it, which releases the history on an exception too. Publish the
router with asDisposable of @xmachines/core/utils:
const plain = createRouter({ routeTree, history });using router = asDisposable(plain, () => plain.destroy());Parameters
| Parameter | Type |
|---|---|
options | { history: BrowserHistory; routeTree: RouteTree; } |
options.history | BrowserHistory |
options.routeTree | RouteTree |