Router
The kea-router plugin provides a nice wrapper around window.History and helps manage the URL
in your application. Use it to listen to route changes or change the URL yourself. There are a
few helpers (actionToUrl and urlToAction) that help track the URL changes, or access the
router directly to manually control the browser history object.
Installation#
First install the kea-router package:
Then install the plugin:
Configuration options#
The plugin takes the following options:
Sample usage#
Use actionToUrl to change the URL in response to actions and urlToAction to dispatch actions when the route changes
Url Pattern#
kea-router uses the url-pattern library under the hood to match
paths. Please see its documentation for all supported options.
UrlToAction#
Search and Hash parameters#
kea-router has built in support for serializing and deserializing search and hash URL parameters, such as:
The second and third parameters to urlToAction are searchParams and hashParams respectively.
These are deserialized objects that you can use directly.
Full example#
For actionToUrl, you may include the search and hash parts directly in the URL or return
an array in the format: [pathname, searchParams, hashParams]. The searchParams and hashParams
can be both strings or objects.
Control the route directly#
Import router to control the router directly in your components
Or in a logic:
Both the push and replace actions accept searchParams and hashParams as their second and
third arguments. You can provide both an object or a string for them. You can also include the
search and hash parts in the url.
Link tag#
Use the included <A> tag to link via the router. This changes the URL via router.actions.push() instead of reloading the entire page.
Listen to location changes#
In case urlToAction is not sufficient for your needs, listen to the locationChanged action to
react to URL changes manually:
Global scene router#
Here's sample code for a global scene router
Utility functions#
kea-router exposes three functions to help manage urls in your app: