Skip to content

Function: createRouteMapFromTree()

API / @xmachines/play-router / createRouteMapFromTree

function createRouteMapFromTree(routeTree, options?): RouteMap;

Defined in: create-route-map-from-tree.ts:33

Creates a RouteMap from the node structure of a RouteTree.

A framework router adapter uses this function when it gives a RouteTree from extractMachineRoutes(), and not when it calls createRouteMap() directly.

The function walks every node, and it collects the pairs { stateId: node.id, path: node.fullPath }. node.fullPath is always the absolute resolved path, for example "/dashboard/overview", and RouteMap needs that path for the match of a browser URL. createRouteMap(machine) behaves in the same way, because it also uses node.fullPath.

Parameters

ParameterTypeDescription
routeTreeRouteTreeA RouteTree, as extractMachineRoutes() returns it.
options?RouteMapOptionsThe optional configuration, for example { cacheSize } to change the size of the LRU cache.

Returns

RouteMap

A RouteMap for each adapter on RouterBridgeBase.

Example

// The preferred form — one call for an XState machine:
import { createRouteMap } from "@xmachines/play-router";
const routeMap = createRouteMap(machine); // it takes an AnyStateMachine
// The two-step form, for a framework adapter that works with a route tree:
import { extractMachineRoutes, createRouteMapFromTree } from "@xmachines/play-router";
const routeTree = extractMachineRoutes(machine);
const routeMap = createRouteMapFromTree(routeTree); // it uses node.fullPath, which is absolute