Skip to content

Function: findRouteByPath()

API / @xmachines/play-router / findRouteByPath

function findRouteByPath(tree, path): RouteNode | undefined;

Defined in: find-route.ts:86

Finds a route node by its URL path

The function looks the route node up by the URL path. It gives you the state ID of a browser URL, for the play.route event of a navigation.

When more than one state holds the same path, for example the root and a second state both at ”/”, the function prefers a node with a route, which means a node with a meta.route field, over a node without one.

The function also matches a pattern of a dynamic route, for example ‘/settings/:section?’.

Parameters

ParameterTypeDescription
treeRouteTreeThe route tree, from extractMachineRoutes
pathstringThe URL path, for example ‘/dashboard’ or ‘/settings/profile’

Returns

RouteNode | undefined

The route node, or undefined when the function finds none

Example

const tree = extractMachineRoutes(machine);
const node = findRouteByPath(tree, "/dashboard");