Solid Router components: A for links, Route for route config, Router for setup, Navigate for redirects, active states, soft navigation.
Complete guide to Solid Router components. Use these components for navigation, routing setup, and redirects.
The <A> component provides enhanced anchor tags with automatic base path support, active states, and soft navigation.
import { A } from "@solidjs/router";
function Navigation() {
return (
<nav>
<A href="/">Home</A>
<A href="/about">About</A>
<A href="/contact">Contact</A>
</nav>
);
}
<A href="/users" activeClass="active" inactiveClass="inactive">
Users
</A>
Default behavior:
active class when href matches current locationinactive class otherwise/users matches /users/123)<A href="/" end>
Home
</A>
end prop:
true: Only active when exact matchfalse: Active for descendants (default)/| Prop | Type | Description |
|---|---|---|
href |
string | Route path (relative or absolute) |
noScroll |
boolean | Disable scroll to top on navigation |
replace |
boolean | Replace history entry (no back button) |
state |
unknown | Push state to history stack |
activeClass |
string | Class when link is active |
inactiveClass |
string | Class when link is inactive |
end |
boolean | Exact match only (no descendants) |
Both <A> and <a> support soft navigation when JavaScript is present. To disable:
<A href="/page" target="_self">Link</A>
Configure routes with preloading and other options.
import { Route } from "@solidjs/router";
<Route path="/users/:id" component={UserProfile} />
<Route
path="/users/:id"
component={UserProfile}
preload={({ params }) => {
void getUserQuery(params.id);
}}
/>
| Prop | Type | Description |
|---|---|---|
path |
string | Route path pattern |
component |
Component | Component to render |
preload |
function | Preload function |
Set up the router with base path and root component.
import { Router, A } from "@solidjs/router";
import { FileRoutes } from "@solidjs/start/router";
import { Suspense } from "solid-js";
function App() {
return (
<Router
root={(props) => (
<>
<nav>
<A href="/">Home</A>
<A href="/about">About</A>
</nav>
<Suspense>{props.children}</Suspense>
</>
)}
>
<FileRoutes />
</Router>
);
}
<Router base="/app">
<FileRoutes />
</Router>
Base path:
<A> components respect base<a> tags don't| Prop | Type | Description |
|---|---|---|
base |
string | Base path for all routes |
root |
Component | Root component wrapper |
data |
object | Router data |
Redirect to a route programmatically.
import { Navigate } from "@solidjs/router";
function Redirect() {
return <Navigate href="/home" />;
}
<Navigate href="/home" replace />
<Navigate href="/home" state={{ from: "/login" }} />
function ProtectedRoute() {
const isAuthenticated = useAuth();
return (
<Show when={isAuthenticated()} fallback={<Navigate href="/login" />}>
<ProtectedContent />
</Show>
);
}
function Navigation() {
return (
<nav>
<A href="/" end activeClass="active">
Home
</A>
<A href="/about" activeClass="active">
About
</A>
<A href="/contact" activeClass="active">
Contact
</A>
</nav>
);
}
function Breadcrumbs() {
const location = useLocation();
const path = location.pathname.split("/").filter(Boolean);
return (
<nav>
<A href="/">Home</A>
<For each={path}>
{(segment, index) => (
<>
<span> / </span>
<A href={`/${path.slice(0, index() + 1).join("/")}`}>
{segment}
</A>
</>
)}
</For>
</nav>
);
}
function ProtectedRoute({ children }) {
const isAuthenticated = useAuth();
return (
<Show when={isAuthenticated()} fallback={<Navigate href="/login" />}>
{children}
</Show>
);
}
<Route
path="/users/:id"
component={UserProfile}
preload={({ params }) => {
void getUserQuery(params.id);
}}
/>
Use <A> for navigation:
Wrap router with Suspense:
Use end prop for root:
/ matching everythingPreload route data:
Use Navigate for redirects: