Options
All
  • Public
  • Public/Protected
  • All
Menu

Module "state/index"

meiosis-routing/state

The state module contains functions for managing routes in the application state.

module

state

Index

Type aliases

Params

Params: Record<string, any>

Route segment params.

Route

Route: RouteSegment[]

A Route is an array of route segments.

RouteParamFn

RouteParamFn: (params?: Record<string, any>) => RouteSegment

A function that creates a RouteSegment with optional params.

Type declaration

Functions

Routing

  • example
    
    // in root component
    const Root = ({ state }) => {
      const routing = Routing(state.route.current);
      const Component = componentMap[routing.localSegment.id];
    
      return (
        <div>
          <Component // other props... // routing={routing} />
        </div>
      );
    };
    
    // in child component
    const Child = ({ state, routing }) => {
      const Component = componentMap[routing.childSegment.id];
      const params = routing.localSegment.params;
    
      return (
        <div>
          <a href={router.toPath(routing.parentRoute())}>...</a>
          <a href={router.toPath(routing.childRoute(Route.Child()))}>...</a>
          <a href={router.toPath(
            routing.siblingRoute([Route.Sibling(), Route.Details()])
          )}>...</a>
    
          <Component // other props... // routing={routing.next()} />
        </div>
      );
    };

    Parameters

    • Default value route: Route = []

      the current route, for example state.route.current.

    • Default value index: number = 0

      the route segment index. This is used internally and you should not specify a value for this parameter.

    Returns RoutingObject

createRouteSegments

  • createRouteSegments(routeNames: string[]): Record<string, RouteParamFn>
  • Creates a Route helper with functions to create route segments.

    example
    
    const Route = createRouteSegments(["Home", "User"]);
    
    Route.Home()
    // { id: "Home", params: {} }
    
    Route.User({ name: "duck" })
    // { id: "User", params: { name: "duck" } }

    Parameters

    • routeNames: string[]

      the list of route names.

    Returns Record<string, RouteParamFn>

    a Route object with constructor functions.

diffRoute

  • Calculates the difference between two routes.

    Parameters

    Returns RouteLookup

    the route representing the segments that are in the from route but not in the to route.

findRouteSegment

  • Looks for a Route segment, regardless of the params, in a route.

    Parameters

    • route: Route | null

      the route to search.

    • id: RouteSegment | string

      the route segment, or just the id of the route segment, to search for in the route.

    Returns RouteSegment | undefined

    the matching Route segment, or undefined if route is empty or a route segment with the given id was not found.

findRouteSegmentWithParams

  • Looks for a route segment, with matching params, in a route.

    Parameters

    • route: Route | null

      the route to search.

    • routeSegmentWithParams: RouteSegment

      the route segment to search for in the route.

    Returns RouteSegment | undefined

    the matching Route segment, or undefined if route is empty or the route segment was not found.

routeTransition

  • Calculates route transitions, providing leave, arrive, and params to indicate the route segments for the route that we are leaving, the route to which we are arriving, and the route for which params have changed, respectively.

    Parameters

    • currentRoute: Route

      the current route, before navigation.

    • nextRoute: Route

      the route to which we are navigating.

    Returns RouteTransition

    an object with leave, arrive, and params properties.

whenPresent

  • whenPresent(value: any, fn: (x: any) => any): any
  • Calls a function with a value only if the value is not null or undefined.

    Parameters

    • value: any

      the value to check.

    • fn: (x: any) => any

      the function to call if value is present.

        • (x: any): any
        • Parameters

          • x: any

          Returns any

    Returns any

    the result of calling fn(value), or null if value is absent.

Generated using TypeDoc