All skills
sergiodxa avatar

/frontend-react-router-best-practices

@9da9532

React Router performance and architecture patterns. Use when writing loaders, actions, forms, routes, or working with React Router data fetching. Triggers on tasks involving React Router routes, data loading, form handling, or route organization.

Use this Skill: https://skilld.dev/gh/sergiodxa/agent-skills/frontend-react-router-best-practices

This session only. Nothing lands on disk.

rulesdata-parent-route-data.md

≈610 tokens on demand. Your agent reads this file only when SKILL.md points to it.

Access Parent Route Data

Use useRouteLoaderData or useMatches to access data from parent routes.

Why

  • Avoids prop drilling through nested routes
  • No need to refetch data already loaded by parent
  • Type-safe access to parent loader data
  • Works with React Router's nested routing model

useRouteLoaderData

Access a specific parent route's data by route ID:

import { useRouteLoaderData } from "react-router";
import type { loader as parentLoader } from "~/routes/_layout/route";

export default function ChildRoute() {
  // Route ID matches the file path without extension
  let parentData =
    useRouteLoaderData<typeof parentLoader>("routes/_layout");

  if (!parentData) return null;

  return <div>Welcome, {parentData.user.name}</div>;
}

useMatches

Access all matched routes and their data:

import { useMatches } from "react-router";

export default function Component() {
  let matches = useMatches();

  // Find a specific match by ID or pathname
  let layoutMatch = matches.find((m) => m.id === "routes/_layout");
  let layoutData = layoutMatch?.data as LayoutLoaderData | undefined;

  return <div>{layoutData?.user.name}</div>;
}

When to Use Which

Scenario Use
Known parent route, need typed data useRouteLoaderData
Search through multiple routes useMatches
Access route handle metadata useMatches
Component used in multiple places useMatches with search

Don't Use Outlet Context

// Bad: Outlet context
export default function Parent() {
  let data = useLoaderData<typeof loader>();
  return <Outlet context={data} />;
}

function Child() {
  let data = useOutletContext<ParentData>();
}

// Good: useRouteLoaderData
function Child() {
  let data = useRouteLoaderData<typeof parentLoader>("routes/parent");
}

Common Parent Routes

// Access root loader data
const rootData = useRouteLoaderData<typeof rootLoader>("root");

// Access authenticated layout data
const layoutData =
  useRouteLoaderData<typeof layoutLoader>("routes/_layout");

// Access settings layout data
const settingsData =
  useRouteLoaderData<typeof settingsLoader>("routes/_.settings");

Source: SKILL.md on GitHub

2 warnings16d5 checks · Risk SAFE
  • Gen Agent Trust Hub16d

    The skill provides a comprehensive set of developer guidelines and best practices for building React Router applications. It emphasizes performance, type safety, and security measures like input validation (Zod), safe redirects, and CSRF protection (Sec-Fetch). No malicious code, obfuscation, or data exfiltration patterns were detected.

  • Socket16d

    1 alert: gptSecurity

  • Snyk16d

    Risk: LOW · No issues

  • Runlayer7mo

    56/56 files flagged

  • ZeroLeaks5mo

    Score: 93/100 · 2 sections analyzed

Signed by skilld at 9da9532. This ties the file your Agent reads to that commit on GitHub. It does not review the instructions.

Last checked against GitHub 2 months ago.

Dormantupdated 8 months ago

README badge

README badge for sergiodxa/agent-skills/frontend-react-router-best-practices