Skip to content

Function: createRouteMap()

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

function createRouteMap(machine, options?): RouteMap;

Defined in: play-router/src/create-route-map.ts:47

Creates a RouteMap from an XState state machine.

The function reads every state with a route, which means each state with a meta.route field. It then builds the lookup structure between a path and a stateId, for both directions. A subclass of RouterBridgeBase uses the map: it converts each change of the browser URL into a play.route actor event, and each actor route into a URL.

Parameters

ParameterTypeDescription
machineAnyStateMachineThe XState v5 state machine, with a meta.route annotation on each state with a route.
options?RouteMapOptionsThe optional configuration. Give { cacheSize } to change the default size of the LRU cache of the parameterized path lookups.

Returns

RouteMap

A RouteMap for each adapter on RouterBridgeBase.

Example

import { createRouteMap } from "@xmachines/play-router";
import { connectRouter } from "@xmachines/play-dom-router";
const routeMap = createRouteMap(myMachine);
const disconnect = connectRouter({ actor, router, routeMap });