Skip to content

Function: createRouteMapFromTree()

API / @xmachines/play-react-router / createRouteMapFromTree

function createRouteMapFromTree(routeTree, options?): RouteMap;

Defined in: play-router/src/create-route-map-from-tree.ts:40

Creates a RouteMap from the node structure of a RouteTree.

A framework router adapter uses this function when it holds a RouteTree, for example from extractMachineRoutes() of @xmachines/play-router/xstate, and not when it calls createRouteMap() of that package 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.

The walk SKIPS the synthetic root node. buildRouteTree makes that node to hold each absolute route, and no state of a machine declares it. createRouteMap skips it as well, through getRoutableRoutes, and the two functions therefore answer the same map for the same tree.

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/xstate";
const routeMap = createRouteMap(machine); // it takes an AnyStateMachine
// The two-step form, for a framework adapter that works with a route tree:
import { createRouteMapFromTree } from "@xmachines/play-router";
import { extractMachineRoutes } from "@xmachines/play-router/xstate";
const routeTree = extractMachineRoutes(machine);
const routeMap = createRouteMapFromTree(routeTree); // it uses node.fullPath, which is absolute