Location
Read the path the browser is on, to set aria-current on the link to the current page or show which page is loading.
On this page
Current path
src/current-path.tsximport { usePathname } from '@k8ordo/router';
export function CurrentPath() {
const pathname = usePathname();
return <p>You are at {pathname}</p>;
}usePathname returns the path the browser is on, with any trailing slash dropped. The path is not decoded: characters such as Japanese come back percent-encoded.
It re-renders only when the path changes. The query string is not returned, and a change to it does not re-render. Read the query with @k8ordo/state.
It needs no route table, so it works the same under @k8ordo/framework.
Current page match
src/products-link.tsximport { href, useMatch } from '@k8ordo/router';
export function ProductsLink() {
const isCurrent = useMatch('/products') !== null;
return (
<a
aria-current={isCurrent ? 'page' : undefined}
href={href('/products')}
>
Products
</a>
);
}useMatch returns the params when the current path matches the pattern, and null when it does not. Use it to set aria-current on a navigation link.
There is no <Link> component, so you set aria-current yourself, as above. useMatch also re-renders only when the path changes.
Patterns ending in /*
matchPath('/products/*', '/products/42'); // {}
matchPath('/products/*', '/products'); // null
matchPath('/products/*', '/products', { inclusive: true }); // {}A pattern followed by /* matches every path below it. Use it when a sidebar needs to know which section is open.
/products/* does not match /products itself. To match /products as well, pass { inclusive: true }.
matchPath makes the same check as useMatch against a path you pass in. /* can only follow a pattern in the route table. With Register augmented, a pattern the table lacks is a type error.
Matching demo
Starts from the path usePathname returns on this page and shows what matchPath answers.
usePathname()/en/router/location
- Call
matchPath('/:locale/router/*', '/en/router/location')- Result
{ locale: 'en' }
Try it
- It starts with the pattern
/:locale/router/*and this page’s path. The result is{ locale: 'en' }: what/*matched is not a param. - Change the path to
/en/router. The result isnull:/*does not match the pattern’s own page. - Turn on “inclusive”. The result is
{ locale: 'en' }again. - Add a
/to the end of the path. The result does not change: the trailing slash is removed before comparing.
Pending path
src/progress.tsximport { usePendingPathname } from '@k8ordo/router';
export function Progress() {
const pending = usePendingPathname();
if (pending === null) return null;
return <p role="status">Loading {pending}…</p>;
}usePendingPathname returns the path of the page being loaded, or null when nothing is loading.
On a page change the URL changes first, and the new page appears once it is ready. On a slow navigation, usePathname already returns the new path while the previous page is still on screen.
It is set as the navigation starts and goes back to null once the new page is on screen, or when the navigation is abandoned or fails to load. An update that changes only the query does not set it, unless the page loads again for it.
Reading params
src/pages/product-page.tsximport { useParams } from '@k8ordo/router';
export function ProductPage() {
const { id } = useParams('/products/:id');
return <h1>Product {id}</h1>;
}A page rendered by <Router> reads its params by passing useParams its own pattern. The type is inferred from the pattern string, and every value is a string.
The pattern also declares that this component renders as the page at that pattern. Rendered under another pattern, it fails with this error:
useParams("/products/:id") rendered under "/products"A component shared by several pages uses useRoute instead, which returns the matched pattern and its params, untyped.
const { pattern, params } = useRoute();Pitfall
Under @k8ordo/framework, useParams and useRoute do not work; a page receives params as a prop (see Parameters).