Options
All
  • Public
  • Public/Protected
  • All
Menu

Class NativeRouter<Ttarget>

It creates a root fragment Rotuer adds and removes child routers and pages as application root.

example
import { Router, Route } from '@smartface/router';
import Image from '@smartface/native/ui/image';
import Color from '@smartface/native/ui/color';

const router = Router.of({
path: "/",
isRoot: true,
to: '/page1',
routes: [
Route.of({
path: "/page1",
build((router, route) => {
const Page1 = require('/pages/Page1');
return new Page1(route.getState().routeData, router);
})
}),
Route.of({
path: "/page2",
build((router, route) => {
const Page2 = require('/pages/Page2');
return new Page2(route.getState().routeData, router);
});
});
]
})
since

1.0.0

Type parameters

  • Ttarget = Page

Hierarchy

  • NativeRouterBase<Ttarget>
    • NativeRouter

Index

Methods

Static of

  • of<Ttarget>(params: RouterParams<Ttarget>): NativeRouter<Ttarget>
  • Create OS specific NativeRouter instance

    static

    Type parameters

    • Ttarget = PageImpl

    Parameters

    • params: RouterParams<Ttarget>

    Returns NativeRouter<Ttarget>

setState

  • setState(state: Partial<RouteState<any>>): void
  • Merges specified state to current route state

    since

    1.0.0

    Parameters

    • state: Partial<RouteState<any>>

    Returns void

getState

  • getState(): RouteState<any>
  • Returns Route's current state

    since

    1.0.0

    Returns RouteState<any>

toJSON

  • toJSON(): { type: string; match: any; routeData: any; routingState: undefined | object; path: string; routes: Route<unknown>[]; state: RouteState<any> & { view: any } }
  • Simple Object representation of the route

    since

    1.0.0

    Returns { type: string; match: any; routeData: any; routingState: undefined | object; path: string; routes: Route<unknown>[]; state: RouteState<any> & { view: any } }

    • type: string
    • match: any
    • routeData: any
    • routingState: undefined | object
    • path: string
    • routes: Route<unknown>[]
    • state: RouteState<any> & { view: any }

toString

  • toString(): string
  • String representation of the route

    since

    1.0.0

    Returns string

setUrl

  • setUrl(url: string): void

clearUrl

  • clearUrl(): void

clearDirty

  • clearDirty(): void

Static getGlobalRouter

  • getGlobalRouter(): HistoryController

Static getHistoryStack

  • getHistoryStack(): any[]

Static getLastHistory

  • getLastHistory(): any

Static getlocationHistoryByIndex

  • getlocationHistoryByIndex(index: number): any
  • Parameters

    • index: number

    Returns any

Static getHistoryByIndex

  • getHistoryByIndex(index: number): any

Static createBlocker

  • createBlocker(fn: Function): (router: Router<unknown>, path: null | string, routeData: null | object, action: HistoryActionType, doneFn: Function) => void
  • Parameters

    • fn: Function

    Returns (router: Router<unknown>, path: null | string, routeData: null | object, action: HistoryActionType, doneFn: Function) => void

      • (router: Router<unknown>, path: null | string, routeData: null | object, action: HistoryActionType, doneFn: Function): void
      • Parameters

        • router: Router<unknown>
        • path: null | string
        • routeData: null | object
        • action: HistoryActionType
        • doneFn: Function

        Returns void

Static getActiveRouter

  • getActiveRouter(): null | Router<unknown>

canGoBack

  • canGoBack(): boolean

onRouteEnter

  • onRouteEnter(route: Route<unknown> | Router<unknown>, action: HistoryActionType): void

setHeaderbarProps

  • setHeaderbarProps(props: IHeaderBar): void

getUrl

  • getUrl(): null | string

getRedirectto

  • getRedirectto(): undefined | string | (() => string)
  • Returns redirection path

    since

    1.0.0

    Returns undefined | string | (() => string)

build

  • build(router: Router<any>): any
  • Builds a route's view. This method is called whenever router is routing into that path. There are some exceptions:

    • going into a tab, which the tab is created before
    • for iOS, goingBack via gesture or headerBar back
    since

    1.0.0

    Parameters

    • router: Router<any>

      Not the root router, the router which the route belongs to.

    Returns any

    view = null - If the route has been built once, the previous view (page) is given. Otherwise it is null. If view is not null, returning the view back makes it singleton.

hasPath

  • hasPath(): boolean
  • If Route has a path or not

    since

    1.0.0

    Returns boolean

matchPath

  • matchPath(url: string): undefined | null | string | { path: string; url: any; isExact: boolean; params: any }
  • Checks if the specified url match to the route path

    Parameters

    • url: string

    Returns undefined | null | string | { path: string; url: any; isExact: boolean; params: any }

getUrlPath

  • getUrlPath(): string
  • Returns route path as string

    since

    1.0.0

    Returns string

getPath

  • getPath(): RoutePath
  • Clones route's path and returns

    since

    1.0.0

    Returns RoutePath

pushHomeRouteBefore

  • pushHomeRouteBefore(path: string): void

findChild

  • findChild(fn: (value: Route<unknown> | Router<unknown>, index: number, obj: (Route<unknown> | Router<unknown>)[]) => unknown): undefined | Route<unknown>
  • Finds and returns child Route or undefined

    since

    1.4.1

    Parameters

    • fn: (value: Route<unknown> | Router<unknown>, index: number, obj: (Route<unknown> | Router<unknown>)[]) => unknown
        • Parameters

          Returns unknown

    Returns undefined | Route<unknown>

historyPreventDefault

  • historyPreventDefault(): void

pushAndBack

  • pushAndBack(url: string, routeData: any): void
  • Parameters

    • url: string
    • routeData: any

    Returns void

hasHome

  • hasHome(): boolean

getCurrentUrl

  • getCurrentUrl(): undefined | string
  • Return current active url

    since

    1.0.0

    Returns undefined | string

listen

  • listen(fn: HistoryListenHandler): () => boolean
  • Adds eventlisteners to listen history changes

    example
    const unlisten = router.listen((location, action) => {
    console.log(`new route location: ${location.url} action : ${action}`);
    });
    since

    1.0.0

    Parameters

    • fn: HistoryListenHandler

    Returns () => boolean

      • (): boolean
      • Returns boolean

addRouteBlocker

  • addRouteBlocker(fn: RouteBlockHandler): () => void
  • Adds route block handler to history. When history is changed in anywhere then the handler intercepts before history is changed.

    example
    const unload = router.addRouteBlocker((path, routeData, action, ok) => {
    alert({
    message: "Would you like to answer?",
    title: "Question", //optional
    buttons: [{
    text: "Yes",
    type: AlertView.Android.ButtonType.POSITIVE,
    onClick: function() {
    ok(true);
    }
    },
    {
    text: "No",
    type: AlertView.Android.ButtonType.NEGATIVE,
    onClick: function() {
    ok(false);
    }
    }
    ]
    });
    });
    since

    1.0.0

    Parameters

    • fn: RouteBlockHandler

    Returns () => void

      • (): void
      • Returns void

getStore

  • getStore(): { saveRoute: any; setState: any; hasRoute: any; findRoute: any; toString: any }

onExit

  • onExit(action: HistoryActionType): void
  • Emits the router is deactivated.

    since

    1.0.0

    example
    Router.of({
    routerDidExit: (router, action) => {
    ...
    }
    })
    emits

    routerDidExit

    Parameters

    • action: HistoryActionType

    Returns void

isAnimated

  • isAnimated(): any

push

  • push(path: string, routeData?: {}, animated?: boolean): NativeRouter<Ttarget>
  • Pushes new history entry with specified path

    since

    1.0.0

    Parameters

    • path: string

      Path or matches of the route

    • routeData: {} = {}
      • animated: boolean = true

      Returns NativeRouter<Ttarget>

    isValidPath

    • isValidPath(path: string): boolean
    • Checks if the specified path is valid or not

      since

      1.0.0

      Parameters

      • path: string

      Returns boolean

    goBack

    • goBack(url?: string | Location, animated?: boolean): undefined | NativeRouter<Ttarget>
    • Rewinds one step the history

      since

      1.0.0

      Parameters

      • Optional url: string | Location

        This is an experimental feature. If you use this feature you should use for same StackRouter stack.

      • animated: boolean = true

      Returns undefined | NativeRouter<Ttarget>

    getLocation

    • getLocation(): undefined | Location
    • Returns last location of history

      since

      1.0.0

      Returns undefined | Location

    getHistoryasArray

    • getHistoryasArray(): string[]
    • Returns History entries as Array

      since

      1.0.0

      Returns string[]

    dispose

    • dispose(): void

    Properties

    map

    map: MapFunction<Route<any>>

    Static currentRouter

    currentRouter: Router<any> | NativeStackRouter<PageImpl>

    Static blocker

    blocker: null | ((router: Router<unknown>, path: null | string, routeData: null | object, action: HistoryActionType, doneFn: Function) => void)

    Optional dispatch

    dispatch?: (location: Location, action: HistoryActionType, target: Router<Ttarget>, fromRouter?: boolean) => void

    Type declaration

      • (location: Location, action: HistoryActionType, target: Router<Ttarget>, fromRouter?: boolean): void
      • Parameters

        • location: Location
        • action: HistoryActionType
        • target: Router<Ttarget>
        • Optional fromRouter: boolean

        Returns void

    emitter

    emitter: { emitRouterDidEnter?: RouteLifeCycleHandler<any>; emitRouterDidExit?: (router: Router<unknown>, action: HistoryActionType) => void; emitRouteWillEnter?: any }

    Type declaration

    • Optional emitRouterDidEnter?: RouteLifeCycleHandler<any>
    • Optional emitRouterDidExit?: (router: Router<unknown>, action: HistoryActionType) => void
        • (router: Router<unknown>, action: HistoryActionType): void
        • Parameters

          • router: Router<unknown>
          • action: HistoryActionType

          Returns void

    • emitRouteWillEnter?:function
      • emitRouteWillEnter(router: Router<unknown>, route: Route<unknown>, view?: any): void

    Accessors

    state

    • get state(): RouteState<any>

    routePath

    • get routePath(): string | (() => string)
    • Helper method to return excat path of the component

      since

      1.0.0

      Returns string | (() => string)

    renderer

    • get renderer(): undefined | default

    matches

    • get matches(): MatchReturn

    historyController

    • get historyController(): undefined | HistoryController

    Constructors

    constructor

    • new NativeRouter<Ttarget>(params: RouteParams<Ttarget>): NativeRouter<Ttarget>

    Events

    routeDidEnter

    • routeDidEnter(route: Route<Ttarget>): void
    • Handles route is matched and displayed

      example
      Route.of({
      routeDidEnter: (router, route) => {
      //...
      }
      })
      emits

      routeDidEnter

      Parameters

      Returns void

    routeDidExit

    • routeDidExit(parent: Route<Ttarget>): void
    • Handles that route is removed by router

      example
      Route.of({
      routeDidExit: (router, route) => {
      //...
      }
      })
      since

      1.0.0

      emits

      routeDidExit

      Parameters

      Returns void

    Generated using TypeDoc