useRouteControl()

For very small applications Bemtv's automatic routing should be sufficient, but if you need more control over each route you can use the useRouteControl() function which accepts a listener/callback as the first argument and triggers it whenever there is an intention to change the route.

The listener will receive as its first argument an instance that has methods and useful properties to manipulate the route and its component.

Once there is an active route controller, Bemtv passes control to it, which decides whether or not to render the route.

import { useRouteControl } from "bemtv";

useRouteControl((routeControl) => {});

to remove the listener just call the function returned by useRouteControl():

import { useRouteControl } from "bemtv";

const remove = useRouteControl((routeControl) => {});

remove();

RouteControl instance

Below are the methods and properties that can be accessed:

RouteControl.name

The component name of the route.

RouteControl.isFirst

Returns true if it is the first route to be accessed and false otherwise.

RouteControl.isRendered

Returns true if the route is already rendered and false otherwise.

RouteControl.load()

When called, it executes the import of the component related to the route and returns the component's import promise.

RouteControl.render()

Renders the route in the Router component.

When calling this method, all procedures related to rendering the route are triggered, including importing the component and changing the browser's URL.

RouteControl.onRender

Allows you add a listener that will be called once the route is rendered.

import { useRouteControl } from "bemtv";

useRouteControl((routeControl) => {
  routeControl.onRender(() => {});
})

To remove the listener just call the function returned by the onRender() method.

RouteControl.cancel()

Cancels the current route and return to the previous one.

Last updated