Overview
TheLink component from the jaspr_router package is a drop-in replacement for the <a> tag that provides client-side navigation when available, falling back to default server-side navigation.
Installation
Constructor
Parameters
The URL to navigate to. Can be a relative or absolute path.
Whether to replace the current route instead of pushing a new one. Only affects client-side routing.
Extra data to attach to the new route. Only affects client-side routing. Must be a primitive serializable value.
Whether to preload the target route when the link is hovered. Only affects client-side routing when using lazy routes.
The
target attribute value applied to the anchor element. Controls where to open the linked document.Values: Target.blank, Target.self, Target.parent, Target.topThe
referrerpolicy attribute value applied to the anchor element. Controls how much referrer information is sent.The
class attribute value applied to the anchor element. Space-separated CSS class names.Inline styles applied to the anchor element.
Other attribute values applied to the anchor element.
Child component to render inside the anchor element. Use either
child or children, not both.Child components to render inside the anchor element. Use either
child or children, not both.An optional key for the component.
Behavior
Client-Side Navigation
When aRouter is available in the component tree:
- Clicking the link prevents the default browser navigation
- Uses client-side routing via
Router.push()orRouter.replace() - No page reload occurs
- Maintains application state
Server-Side Fallback
When noRouter is available:
- Falls back to standard
<a>tag behavior - Full page reload on navigation
- Works without JavaScript
Preloading
Whenpreload is true (default):
- The target route is preloaded when the link is hovered
- Only works with
LazyRoutes - Improves perceived navigation speed
- Only affects client-side routing
Example Usage
Basic Link
Link with Children
Styled Link
Replace Navigation
External Link
Link with Extra Data
Disable Preloading
Navigation Menu
Active Link Styling
Breadcrumb Navigation
Button-Style Link
Card with Link
Download Link
Accessibility
TheLink component renders a standard <a> tag, so it inherits all the accessibility benefits:
- Keyboard navigable (Tab key)
- Works with screen readers
- Semantic HTML
- Supports ARIA attributes via the
attributesparameter
Best Practices
Use semantic HTML inside links
Use semantic HTML inside links
Keep link content simple and descriptive. Avoid nesting interactive elements.
Disable preload for expensive routes
Disable preload for expensive routes
For routes that load heavy resources, consider disabling preload.
Use target wisely
Use target wisely
Only use
target="_blank" for external links. Internal links should stay in the same tab.Provide clear link text
Provide clear link text
Avoid generic text like “click here”. Use descriptive labels.
Important Notes
The
Link component automatically detects if a Router is available in the component tree. If no router is found, it falls back to standard anchor tag behavior.Preloading only affects lazy routes. Routes loaded eagerly won’t benefit from the preload functionality.
Related Components
- Router - Router component for managing routes
- StatelessComponent - Base stateless component class