Router
The router provides client-side navigation capabilities in COSMOS RSC applications.useRouter
A React hook that returns the router instance for programmatic navigation.Return Value
TheuseRouter hook returns a router object with the following methods:
Navigates to a new URL using the push history action.Signature:
(url: string) => voidParameters:url- The destination URL path
Error Handling
The hook throws an error if called outside of a router context:RouterContext
The underlying React context that provides the router instance. Most applications should use theuseRouter hook instead of accessing this context directly.
Navigation API Integration
COSMOS RSC’s router is built on top of the browser’s Navigation API, which provides:- Programmatic navigation with
router.push() - Back/forward navigation support
- Navigation type detection (push, replace, traverse)
- Navigation transition types for animations
Navigation Types
When navigating, COSMOS RSC automatically adds transition types that can be used with theNavigationTransition component:
navigation-push- New page navigationnavigation-replace- Replacing current pagenavigation-forward- Forward browser navigationnavigation-back- Back browser navigationnavigation-traverse- Generic back/forward navigation
Browser Compatibility
The router requires the Navigation API, which is available in:- Chrome/Edge 102+
- Safari 16.4+
- Firefox (behind flag)