useHref
Resolves a URL against the current location.Signature
Parameters
To
required
The path to resolve. Can be a string or a partial location object.
'route' | 'path'
Controls relative routing logic. Defaults to
"route" so routing is relative to the route tree. Set to "path" to make relative routing operate against path segments.Returns
string
The resolved href string that can be used in an anchor tag or for other purposes.
Usage
Basic usage
Resolve relative paths
With location object
Share links
Custom link component
Download links
Common Patterns
QR code generation
Social sharing
Email links
Canonical URLs
Copy to clipboard
Relative routing types
With basename
Navigation hints
Type Safety
Important Notes
Must be inside Router
useHref must be called within a <Router> component context:
Use Link for navigation
For in-app navigation, prefer<Link> over using useHref with <a> tags:
useHref when you need the URL string for purposes other than navigation (sharing, QR codes, etc.).
Related
Link- Declarative navigation componentuseNavigate- Navigate programmaticallyuseResolvedPath- Get path object without creating hrefuseLocation- Access current location