The router provides extensive callback and event mechanisms to hook into the routing lifecycle, implement custom logic, and respond to navigation events.
- Events are fired by the Router component (onrouteLoading, onrouteLoaded, etc.) to notify your app of routing state changes
- Callbacks are functions you provide (conditions, authorizationCallback, etc.) that the router calls to make decisions
Router Component Events
These event handler props are passed to the <Router> component and fire
at specific points in the routing lifecycle.
onRouteLoading
Fires when a route starts loading, after route match but before conditions are checked and the component is loaded.
Event Detail Structure
Usage Example
onRouteLoaded
Fires after a route successfully loads and all conditions pass. Called for both single-component and zone-based routes.
Event Detail Structure (Single Component)
Event Detail Structure (Zone-Based Routes)
Usage Example
onConditionsFailed
Fires when route conditions (guards) fail, preventing navigation to the requested route.
Event Detail Structure
Usage Example
onNotFound
Fires when no route matches the current location (404 error).
Event Detail Structure
Usage Example
Navigation Guard Callbacks
Navigation guards allow you to prevent users from leaving a route when there are unsaved changes or other conditions.
registerBeforeLeave
Register a callback that runs before navigating away from the current route. Throw NavigationCancelledError to prevent navigation.
Callback Signature
Usage Example
createDirtyCheckGuard helper for simpler
dirty state checking:Route Condition Callbacks
Conditions are async functions that run before a route's component loads. Return false to block navigation.
conditions Array
Pass an array of condition functions to wrap(). Each function receives
route details and must return a boolean.
Callback Signature
Usage Example
Permission System Callbacks
The permission system requires configuration callbacks to check user permissions and handle unauthorized access.
configurePermissions
Configure the permission system with three required callbacks:
checkPermissions
getCurrentUser
onUnauthorized
Complete Configuration Example
authorizationCallback
Resource-based authorization callback for checking access to specific resources (e.g., documents, projects).
Callback Signature
Usage Example
Error Handler Callbacks
The global error handler provides callbacks for logging errors and implementing custom recovery strategies.
onError
Called whenever an unhandled error occurs. Use this to send errors to monitoring services like Sentry.
Callback Signature
Usage Example
onRecover
Called when using strategy: 'custom' to implement custom error recovery
logic.
Callback Signature
Usage Example
'navigateSafe'- Navigate to safe route (default)'restart'- Reload the page (with loop prevention)'showError'- Display error component'custom'- Call onRecover callback
Lifecycle & Execution Order
Understanding the order in which callbacks execute is crucial for proper routing logic.
Navigation Lifecycle
When navigating to a new route, callbacks execute in this order:
- registerBeforeLeave handlers (if leaving a route)
- All registered handlers execute
- Navigation cancelled if any throws NavigationCancelledError
- onrouteLoading event fires
- Route matched but not yet loaded
- Route matching
- Pattern matching against current location
- Parameter extraction
- conditions array execution
- In hierarchical mode: parent conditions execute first
- All conditions must return true to proceed
- checkPermissions (if using permission system)
- Fast role-based check
- In hierarchical mode: parent permissions checked first
- authorizationCallback (if defined)
- Slow resource-based check (API calls)
- Only runs if permissions pass
- In hierarchical mode: parent authorization runs first
- Component loading
- Async component import resolves
- Component instantiated
- onrouteLoaded event fires (success path)
- Route fully loaded and rendered
- If no route matches → onNotFound fires
- If conditions/permissions fail → onconditionsFailed fires
- If unhandled error occurs → onError callback (if configured)
Hierarchical Mode Execution
When hierarchical routes are enabled, parent callbacks execute before child callbacks:
This matches filesystem security where you need access to all parent directories to reach a nested file.
Quick Reference
Summary of all callbacks and events with their primary use cases.
| Callback/Event | Type | When Called | Primary Use Case |
|---|---|---|---|
onRouteLoading | Event | Route starts loading | Show loading indicator, start timer |
onRouteLoaded | Event | Route successfully loads | Analytics, hide loading, log page view |
onConditionsFailed | Event | Route conditions fail | Redirect to unauthorized page |
onNotFound | Event | No route matches (404) | Error tracking, analytics, logging |
registerBeforeLeave | Callback | User tries to leave route | Prevent navigation with unsaved changes |
conditions | Callback | Before component loads | Authentication, feature flags |
checkPermissions | Callback | Permission check needed | Role-based access control |
getCurrentUser | Callback | User info needed | Get current user object |
onUnauthorized | Callback | Permission denied | Redirect to login/unauthorized |
authorizationCallback | Callback | Resource access check | API-based authorization |
onError | Callback | Unhandled error occurs | Send to Sentry, log to service |
onRecover | Callback | Custom recovery strategy | Custom error recovery logic |
Related Documentation
Guards & Conditions
Learn more about implementing route guards and navigation protection.
View Guards GuidePermissions System
Detailed guide to role-based and resource-based access control.
View Permissions Guide