Next generation React routing library with accessible navigation and focus management.
npx @tessl/cli install tessl/npm-reach--router@1.3.0Reach Router is a React routing library that provides accessible, declarative routing solutions for React applications. It features automatic focus management, dynamic route matching, and seamless navigation with built-in accessibility support.
npm install @reach/routerimport { Router, Link, navigate } from "@reach/router";For CommonJS:
const { Router, Link, navigate } = require("@reach/router");import React from "react";
import { Router, Link } from "@reach/router";
const Home = () => <div>Welcome Home!</div>;
const About = () => <div>About Us</div>;
const User = ({ userId }) => <div>User: {userId}</div>;
function App() {
return (
<div>
<nav>
<Link to="/">Home</Link>
<Link to="/about">About</Link>
<Link to="/users/123">User 123</Link>
</nav>
<Router>
<Home path="/" />
<About path="/about" />
<User path="/users/:userId" />
</Router>
</div>
);
}Reach Router is built around several key components:
Essential routing components for building navigable React applications with automatic route matching and focus management.
function Router(props: {
basepath?: string;
primary?: boolean;
children: React.ReactNode;
component?: string | React.ComponentType;
[key: string]: any;
}): React.ReactElement;Link components and programmatic navigation functions for moving between routes with accessibility features and state management.
const Link: React.ForwardRefExoticComponent<{
to: string;
state?: any;
replace?: boolean;
getProps?: (props: LinkProps) => object;
innerRef?: React.Ref<HTMLAnchorElement>;
} & React.AnchorHTMLAttributes<HTMLAnchorElement> & React.RefAttributes<HTMLAnchorElement>>;
function navigate(to: string | number, options?: {
state?: any;
replace?: boolean;
}): Promise<void>;Location providers and consumer components for accessing current location state and navigation functions in React components.
function LocationProvider(props: {
history?: History;
children: React.ReactNode | ((context: LocationContext) => React.ReactNode);
}): React.ReactElement;
function Location(props: {
children: (context: LocationContext) => React.ReactNode;
}): React.ReactElement;
interface LocationContext {
location: Location;
navigate: (to: string | number, options?: NavigateOptions) => Promise<void>;
}Modern React hooks for accessing routing state and navigation functions in functional components.
function useLocation(): Location;
function useNavigate(): (to: string | number, options?: NavigateOptions) => Promise<void>;
function useParams(): Record<string, string> | null;
function useMatch(path: string): MatchResult | null;History utilities for creating custom history instances and managing navigation state in different environments.
function createHistory(source: LocationSource, options?: any): History;
function createMemorySource(initialPath?: string): LocationSource;
const globalHistory: History;Components and utilities for rendering routes on the server with static location context.
function ServerLocation(props: {
url: string;
children: React.ReactNode;
}): React.ReactElement;Advanced routing patterns including redirects, conditional rendering, and route matching utilities.
function Redirect(props: {
from?: string;
to: string;
replace?: boolean;
state?: any;
noThrow?: boolean;
}): React.ReactElement;
function Match(props: {
path: string;
children: (props: MatchProps) => React.ReactNode;
}): React.ReactElement;
function matchPath(path: string, uri: string): MatchResult | null;
function isRedirect(error: any): boolean;
function redirectTo(to: string): never;Path resolution and routing utility functions for advanced routing scenarios and custom integrations.
function resolve(to: string, base: string): string;
function insertParams(path: string, params: Record<string, any>): string;
function validateRedirect(from: string, to: string): boolean;
function shallowCompare(obj1: object, obj2: object): boolean;interface Location {
pathname: string; // URI encoded/decoded
search: string;
hash: string;
href?: string; // Optional in non-browser environments
origin?: string; // Optional in non-browser environments
protocol?: string; // Optional in non-browser environments
host?: string; // Optional in non-browser environments
hostname?: string; // Optional in non-browser environments
port?: string; // Optional in non-browser environments
state: any;
key: string; // Defaults to "initial"
}
interface NavigateOptions {
state?: any;
replace?: boolean;
}
interface MatchResult {
route: RouteInfo;
params: Record<string, string | string[]>; // Can contain arrays for splat routes
uri: string; // Always normalized with leading slash
}
interface RouteInfo {
path: string;
default?: boolean;
value: React.ReactElement;
}
interface History {
location: Location;
transitioning: boolean;
listen(listener: (event: { location: Location; action: string }) => void): () => void;
navigate(to: string | number, options?: NavigateOptions): Promise<void>;
}
interface LocationSource {
location: {
pathname: string;
search: string;
hash?: string;
href?: string;
origin?: string;
protocol?: string;
host?: string;
hostname?: string;
port?: string;
};
history: {
state: any;
pushState(state: any, title: string | null, url: string): void;
replaceState(state: any, title: string | null, url: string): void;
go(delta: number): void;
};
addEventListener(type: string, listener: () => void): void;
removeEventListener(type: string, listener: () => void): void;
}
interface LinkProps {
isCurrent: boolean;
isPartiallyCurrent: boolean;
href: string;
location: Location;
}
interface MatchProps {
navigate: (to: string | number, options?: NavigateOptions) => Promise<void>;
location: Location;
match: MatchResult | null;
}