existential-birds/beagle / plugins/beagle-react/skills/react-router-v7
react-router-v7
VerifiedReact Router v7 best practices for data-driven routing. Use when implementing routes, loaders, actions, Form components, fetchers, navigation guards, protected routes, or URL search params. Triggers on createBrowserRouter, RouterProvider, useLoaderData, useActionData, useFetcher, NavLink, Outlet.
| File | Kind | Size |
|---|---|---|
| ACTIONS.md | markdown | 4.1 KB |
| ADVANCED.md | markdown | 4.0 KB |
| LOADERS.md | markdown | 2.1 KB |
| NAVIGATION.md | markdown | 2.7 KB |
| SKILL.md | skill md | 4.0 KB |
| references/actions.md | markdown | 4.6 KB |
| references/advanced.md | markdown | 5.2 KB |
| references/loaders.md | markdown | 2.4 KB |
| references/navigation.md | markdown | 3.1 KB |
references/navigation.md
View on GitHub# Navigation Patterns
## Contents
- [NavLink (Active Styling)](#navlink-active-styling)
- [Link (No Active Styling)](#link-no-active-styling)
- [Programmatic Navigation](#programmatic-navigation)
- [Redirect in Loaders](#redirect-in-loaders)
- [Pending UI (Navigation State)](#pending-ui-navigation-state)
- [Form Submission State](#form-submission-state)
- [Index Routes](#index-routes)
- [Layout Routes (No Path)](#layout-routes-no-path)
- [Navigate Component](#navigate-component)
---
## NavLink (Active Styling)
```tsx
import { NavLink } from "react-router";
<NavLink to="/messages" end>
Messages
</NavLink>
// CSS styling
a.active { color: red; }
a.pending { animation: pulse 1s infinite; }
// Callback styling
<NavLink
to="/messages"
className={({ isActive, isPending }) =>
isActive ? "active" : isPending ? "pending" : ""
}
>
Messages
</NavLink>
```
## Link (No Active Styling)
```tsx
import { Link } from "react-router";
<Link to="/login">Login</Link>
<Link to={{ pathname: "/search", search: "?q=term" }}>Search</Link>
```
## Programmatic Navigation
```tsx
import { useNavigate } from "react-router";
function Component() {
const navigate = useNavigate();
// Use sparingly - only for non-user-initiated navigation
useEffect(() => {
if (inactivityTimeout) {
navigate("/logout");
}
}, [inactivityTimeout]);
// Or with options
navigate("/dashboard", { replace: true });
navigate(-1); // Go back
}
```
## Redirect in Loaders
```tsx
import { redirect } from "react-router";
export async function loader({ request }) {
const user = await getUser(request);
if (!user) {
return redirect("/login");
}
return { user };
}
```
## Pending UI (Navigation State)
```tsx
import { useNavigation } from "react-router";
function Root() {
const navigation = useNavigation();
const isNavigating = navigation.state !== "idle";
return (
<div>
{isNavigating && <GlobalSpinner />}
<Outlet />
</div>
);
}
```
## Form Submission State
```tsx
function Component() {
const navigation = useNavigation();
const isSubmitting = navigation.formAction === "/recipes/new";
return (
<Form method="post" action="/recipes/new">
<button type="submit">
{isSubmitting ? "Saving..." : "Create Recipe"}
</button>
</Form>
);
}
```
## Index Routes
```tsx
createBrowserRouter([
{
path: "/dashboard",
Component: Dashboard,
children: [
{ index: true, Component: DashboardHome }, // Renders at /dashboard
{ path: "settings", Component: Settings }, // Renders at /dashboard/settings
],
},
]);
```
## Layout Routes (No Path)
```tsx
createBrowserRouter([
{
Component: MarketingLayout, // No path, just layout wrapper
children: [
{ index: true, Component: Home },
{ path: "contact", Component: Contact },
],
},
]);
```
## Navigate Component
```tsx
import { Navigate, useLocation } from "react-router";
function RequireAuth({ children }) {
const auth = useAuth();
const location = useLocation();
if (!auth.user) {
return <Navigate to="/login" state={{ from: location }} replace />;
}
return children;
}
```
Related skills
| Skill | Stars | Updated | Works with | Status | Install |
|---|---|---|---|---|---|
| brainstormingobra/superpowersYou MUST use this before any creative work - creating features, building components, adding functionality, modifying behavior, or planning anything new, in software or out of it (a talk, a business, a renovation). | Stars: 322k | Updated | Claude CodeCodex | Verified | Install |
| brand-guidelinesanthropics/skillsApplies Anthropic's official brand colors and typography to any sort of artifact that may benefit from having Anthropic's look-and-feel. Use it when brand colors or style guidelines, visual formatting, or company design standards apply. | Stars: 197k | Updated | Claude CodeCodex | Verified | Install |
| frontend-designanthropics/skillsGuidance for distinctive, intentional visual design when building new UI or reshaping an existing one. Helps with aesthetic direction, typography, and making choices that don't read as templated defaults. | Stars: 197k | Updated | Claude CodeCodex | Verified | Install |
| mcp-builderanthropics/skillsGuide for creating high-quality MCP (Model Context Protocol) servers that enable LLMs to interact with external services through well-designed tools. Use when building MCP servers to integrate external APIs or services, whether in Python (FastMCP) or Node/TypeScript (MCP SDK). | Stars: 197k | Updated | Claude CodeCodex | Verified | Install |