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.
#
InstallationFirst install the kea-router
package:
Then install the plugin:
#
Configuration optionsThe plugin takes the following options:
#
Sample usageUse actionToUrl
to change the URL in response to actions and urlToAction
to dispatch actions when the route changes
#
Url Patternkea-router
uses the url-pattern library under the hood to match
paths. Please see its documentation for all supported options.
#
UrlToAction#
Search and Hash parameterskea-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 exampleFor 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 directlyImport 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 tagUse 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 changesIn case urlToAction
is not sufficient for your needs, listen to the locationChanged
action to
react to URL changes manually:
#
Global scene routerHere's sample code for a global scene router
#
Utility functionskea-router
exposes three functions to help manage urls in your app: