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 |
ADVANCED.md
View on GitHub# Advanced Patterns
## Error Boundaries
### Root Error Boundary (Required)
```tsx
import { useRouteError, isRouteErrorResponse } from "react-router";
function RootErrorBoundary() {
const error = useRouteError();
if (isRouteErrorResponse(error)) {
return (
<>
<h1>{error.status} {error.statusText}</h1>
<p>{error.data}</p>
</>
);
} else if (error instanceof Error) {
return (
<div>
<h1>Error</h1>
<p>{error.message}</p>
<pre>{error.stack}</pre>
</div>
);
} else {
return <h1>Unknown Error</h1>;
}
}
createBrowserRouter([
{
path: "/",
ErrorBoundary: RootErrorBoundary,
Component: Root,
},
]);
```
### Throwing Errors in Loaders
```tsx
import { data } from "react-router";
export async function loader({ params }) {
const record = await db.getRecord(params.id);
if (!record) {
throw data("Record Not Found", { status: 404 });
}
return record;
}
```
### Nested Error Boundaries
```tsx
createBrowserRouter([
{
path: "/app",
ErrorBoundary: AppErrorBoundary,
children: [
{
path: "invoices/:id",
ErrorBoundary: InvoiceErrorBoundary,
Component: Invoice,
},
],
},
]);
```
## Protected Routes
### Component-Based Protection
```tsx
import { Navigate, useLocation } from "react-router";
function RequireAuth({ children }: { children: JSX.Element }) {
const auth = useAuth();
const location = useLocation();
if (!auth.user) {
return <Navigate to="/login" state={{ from: location }} replace />;
}
return children;
}
// Route configuration
{
path: "/protected",
element: (
<RequireAuth>
<ProtectedPage />
</RequireAuth>
),
}
// In login handler - redirect back
function LoginPage() {
const navigate = useNavigate();
const location = useLocation();
const from = location.state?.from?.pathname || "/";
function handleLogin() {
auth.signin(() => {
navigate(from, { replace: true });
});
}
}
```
### Middleware (Framework Mode)
```tsx
import { redirect } from "react-router";
async function authMiddleware({ context, request }) {
const userId = getUserId(request);
if (!userId) {
throw redirect("/login");
}
const user = await getUserById(userId);
context.set(userContext, user);
}
createBrowserRouter([
{
path: "/dashboard",
middleware: [authMiddleware],
Component: Dashboard,
},
]);
```
## Lazy Loading / Code Splitting
### Data Mode Lazy Loading
```tsx
createBrowserRouter([
{
path: "/app",
lazy: async () => {
const [Component, loader] = await Promise.all([
import("./app"),
import("./app-loader"),
]);
return { Component, loader };
},
},
]);
```
### Declarative Mode Lazy Loading
```tsx
import React from "react";
const About = React.lazy(() => import("./pages/About"));
<Routes>
<Route
path="about"
element={
<React.Suspense fallback={<>Loading...</>}>
<About />
</React.Suspense>
}
/>
</Routes>
```
## Common Route Patterns
### Optional Segments
```tsx
{ path: ":lang?/categories" } // Optional dynamic segment
{ path: "users/:userId/edit?" } // Optional static segment at end
```
### Catch-All / Splat Routes
```tsx
{ path: "files/*" }
// Access splat in loader
loader: ({ params }) => {
const filePath = params["*"]; // "path/to/file.txt"
}
```
### Multiple Params
```tsx
{ path: "users/:userId/posts/:postId" }
// params.userId, params.postId available in loader/component
```
## Index vs Path Routes
```tsx
createBrowserRouter([
{
path: "/dashboard",
Component: Dashboard,
children: [
// Index route - renders when parent path matches exactly
{ index: true, Component: DashboardHome },
// Path route - renders at parent + path
{ path: "settings", Component: Settings },
{ path: "profile", Component: Profile },
],
},
]);
```
**Index renders at**: `/dashboard`
**Settings renders at**: `/dashboard/settings`
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: 198k | 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: 198k | 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: 198k | Updated | Claude CodeCodex | Verified | Install |