API / Functions
isActive() function
Whether a route is on the current page’s path. A section stays active while any page under it shows, which is what a menu needs to mark where you are.
Signature
isActive(target, args?): boolean
Parameters
A route name or a Route. A section’s name means the section, not its index page.
nullParams and query the page must also have. Only the keys you pass are compared.
Returns
true when the route is on the committed page’s path and every key in args matches.
Compared with isCurrent()
isCurrent() asks whether a route is the page itself. On /team/acme/members:
| Call | isActive | isCurrent |
|---|---|---|
('team') | true | false |
('members') | true | true |
('team', { org: 'acme' }) | true | false |
('team', { org: 'globex' }) | false | false |
('settings') | false | false |
Mark the current link
Set aria-current from it, so the active state is announced and you can style it from CSS.
import { href, isActive } from 'nav-router';
const link = (name, label) => `
<a href="${href(name)}" ${isActive(name) ? 'aria-current="page"' : ''}>
${label}
</a>`;import { href, isActive } from 'nav-router';
const NavLink = ({ name, children }) => (
<a
href={href(name)}
aria-current={isActive(name) ? 'page' : undefined}
>
{children}
</a>
);import { href, isActive } from 'nav-router';
const NavLink = (props) => (
<a
href={href(props.name)}
aria-current={isActive(props.name) ? 'page' : undefined}
>
{props.children}
</a>
);It reads the committed page
During a navigation that hasn’t committed yet, isActive() still answers for the page on screen. To light the link being opened, read router.pending.
Related
isCurrent(): the page itself, not its sections.canAccess(): whether the user may open a route, for hiding links.href(): the URL for a route.