Documentation Index
Fetch the complete documentation index at: https://mintlify.com/jpcutshall/svelte5-router/llms.txt
Use this file to discover all available pages before exploring further.
useRouter() returns the RouterContext object provided by the nearest ancestor <Router> component. It exposes reactive stores that track which route is currently active, what base path the router is mounted on, and the derived base for child routers. It also provides the registerRoute and unregisterRoute functions that <Route> uses to announce itself to the router—most application code will never need to call these directly. Like all context hooks, useRouter() must be called during Svelte component initialisation.
Signature
If there is no ancestor
<Router> in the component tree, useRouter() returns undefined. Always guard against this when building reusable components that might be rendered outside a <Router>.Return value
useRouter() returns a RouterContext object (or undefined when called outside a router tree).
RouterContext
A Svelte readable store that holds information about the route the router has selected as the best match. Updated on every navigation.
A readable store representing the base that this router was initialised with. For a top-level router this is the
basepath prop value (default "/"). For nested routers it inherits from the parent router’s routerBase.A derived readable store that reflects the effective base for child routers. It equals
base when no route is active; once a route is matched it is derived from that route’s path and URI so that nested <Router> components resolve paths correctly.Registers a route configuration with the parent router so it can be scored and matched against the current location. Called automatically by
<Route> on mount.Removes a previously registered route from the parent router. Called automatically by
<Route> on destroy.Examples
Reading the active route URI
Use$router.activeRoute to read information about what is currently displayed. This is useful for analytics, breadcrumbs, or conditionally rendering UI based on which route is active.
Reading path parameters from the active route
Checking the base path in a nested context
When building reusable widget components that may be placed inside a nested<Router>, you can inspect the current base to construct correct links.