Przejdź do treści
Biblioteka skilli
Programowanie · Budowanie i kod

react-router-v7

/react-router-v7

Opisuje konwencje trybu framework React Router v7: moduły tras, loadery i akcje, progresywne ulepszanie, ErrorBoundary tras, rewalidacja danych oraz migracja z Remix.

Repozytorium
skills ↗
Licencja
Apache-2.0
Ostatnia zmiana repozytorium
08.10.2026

Ocena i bezpieczeństwo

100/ 100Klasa: dobry
Format25 / 25
Frontmatter z nazwą i opisem, poprawna nazwa, opis do 1024 znaków, nazwa zgodna z folderem.
Odkrywalność25 / 25
Opis mówi, kiedy użyć skilla, ma sensowną długość i jest w 3. osobie.
Treść35 / 35
Co najmniej 150 słów i do 500 linii, nagłówki, listy kroków, przykłady albo kod.
Higiena15 / 15
Bez zaślepek typu TODO, bez ścieżek Windows, z licencją.
Ryzykobrak sygnałów w skanie
Pliki w paczce
1
Skrypty
0
Komentarze HTML
0
Adresy URL w SKILL.md
1
Przestarzałość

Brak sygnałów. Repozytorium zmienione 08.10.2026, bez odwołań do wycofanych modeli i API.

Licencja

Apache-2.0, otwarta: można używać i zmieniać, z zachowaniem licencji i informacji o zmianach.

Co robi i kiedy

Opisuje konwencje trybu framework React Router v7: moduły tras, loadery i akcje, progresywne ulepszanie, ErrorBoundary tras, rewalidacja danych oraz migracja z Remix.

Oryginalny opis (EN)

React Router v7 framework mode conventions for route modules, data loaders, actions, and progressive enhancement. Use when building file-based routes with loaders/actions, handling form submissions with Form/useFetcher/useSubmit, adding route ErrorBoundary components, revalidating data after mutations, or migrating Remix-style route modules to React Router v7.

Jak działa

  1. Workflow for Building a Route Module
  2. Route Modules
  3. Data Loading
  4. Mutations
  5. Navigation and State
  6. TypeScript and Testing
  7. Common Mistakes
Sekcje z nagłówków SKILL.md, w oryginale · 126 linii, 1 050 słów
Podgląd SKILL.md
---
name: react-router-v7
description: "React Router v7 framework mode conventions for route modules, data loaders, actions, and progressive enhancement. Use when building file-based routes with loaders/actions, handling form submissions with Form/useFetcher/useSubmit, adding route ErrorBoundary components, revalidating data after mutations, or migrating Remix-style route modules to React Router v7."
metadata:
  maintainer: Mindrally
  source: https://github.com/Mindrally/skills
---

# React Router v7

React Router v7 framework mode unifies routing, data loading, and mutations into route modules, each exporting a `loader`, `action`, `default` component, and `ErrorBoundary` for a single URL segment.

## Workflow for Building a Route Module

1. **Create the route file** — Add a file under `app/routes/` (or your configured routes directory) for the URL segment, using nested folders/files for layouts and dynamic segments.
2. **Export a loader** — Fetch the data the route needs before render; validate route params and search params at the top of the loader.
3. **Export a component** — Read loader data via `useLoaderData()` and render UI; keep the component focused on presentation.
4. **Export an action** — Handle `POST`/`PUT`/`DELETE` submissions from `<Form>` or `useFetcher`; return typed data (including validation errors) instead of throwing for expected failures.
5. **Export an ErrorBoundary** — Handle thrown errors (400s, 500s, unexpected exceptions) locally so one route's failure doesn't take down the whole app.
6. **Wire up navigation** — Use `<Form>`, `useFetcher`, and `useSubmit` for mutations so the app works with JavaScript disabled and gets optimistic UI when it's enabled.
7. **Test the module** — Write tests for the loader, action, validation failures, and error boundary independently of the rendered component tree.

## Route Modules

- Use route modules as the boundary for route UI, loader data, actions, metadata, and error boundaries — a route module is the unit of code splitting and data loading.
- Keep route modules small; move shared UI to `app/components/` and reusable data access to a services layer (e.g. `app/services/posts.server.ts`).
- Prefer file-based routing in framework mode when the project is configured for it; use the explicit `routes.ts` config for advanced/dynamic route trees.
- Use nested routes for shared layouts and progressive disclosure — a parent route's `<Outlet />` renders the matched child.
- Export route-specific `ErrorBoundary` components for recoverable route failures instead of relying on a single app-wide boundary.

```tsx
// app/routes/posts.$postId.tsx
import { Form, useLoaderData, useNavigation } from "react-router";
import type { Route } from "./+types/posts.$postId";
import { getPost, updatePost } from "~/services/posts.server";

export async function loader({ params }: Route.LoaderArgs) {
  const post = await getPost(params.postId);
  if (!post) {
    throw new Response("Not Found", { status: 404 });
  }
  return { post };
}

export async function action({ request, params }: Route.ActionArgs) {
  const formData = await request.formData();
  const title = String(formData.get("title") ?? "");

  if (title.trim().length === 0) {
    return { ok: false as const, errors: { title: "Title is required" } };
  }

  await updatePost(params.postId, { title });
  return { ok: true as const, errors: null };
}

export default function PostDetail() {
  const { post } = useLoaderData<typeof loader>();
  const navigation = useNavigation();
  const isSubmitting = navigation.state === "submitting";

  return (
    <Form method="post">
      <input name="title" defaultValue={post.title} />
      <button type="submit" disabled={isSubmitting}>
        {isSubmitting ? "Saving..." : "Save"}
      </button>
    </Form>
  );
}

export function ErrorBoundary({ error }: Route.ErrorBoundaryProps) {
  if (error instanceof Response && error.status === 404) {
    return <p>Post not found.</p>;
  }
  return <p>Something went wrong loading this post.</p>;
}
```

## Data Loading

- Use loaders for route data that should be available before render — this avoids loading spinners for data the page can't function without.
- Keep loaders deterministic and side-effect free; a loader should read data, not write it.
- Validate params and search params at the loader boundary (e.g. parse and range-check a `page` query param) rather than trusting them downstream.
- Return typed data and consume it through `useLoaderData<typeof loader>()` rather than duplicating fetch logic in the component.
- Use deferred or streaming patterns (`defer`, or returning promises for `<Await>`) only when they measurably improve perceived performance — not by default.

## Mutations

- Use actions for route mutations and form submissions; an action runs on `POST`/`PUT`/`PATCH`/`DELETE` navigations and fetcher submissions to that route.
- Prefer `<Form>`, `useFetcher`, and `useSubmit` for progressive enhancement — these work without client-side JavaScript and layer on optimistic UI when it's available.
- Revalidate affected loader data after mutations; React Router automatically revalidates loaders on the current route tree after an action completes, so avoid manually refetching in `useEffect`.
- Handle validation errors as typed action data (`return { errors }`) instead of throwing generic exceptions — reserve thrown `Response`s for actual HTTP-status failures (404, 401, 500).
- Keep server-only secrets and privileged operations (API keys, direct DB writes) out of client actions — perform them in `.server.ts` modules that are stripped from the client bundle.

```tsx
// Optimistic UI with useFetcher
function LikeButton({ postId, liked }: { postId: string; liked: boolean }) {
  const fetcher = useFetcher();
  const optimisticLiked = fetcher.formData
    ? fetcher.formData.get("liked") === "true"
    : liked;

  return (
    <fetcher.Form method="post" action={`/posts/${postId}/like`}>
      <input type="hidden" name="liked" value={String(!optimisticLiked)} />
      <button type="submit">{optimisticLiked ? "Unlike" : "Like"}</button>
    </fetcher.Form>
  );
}
```

## Navigation and State

- Store shareable state in URL params or search params (filters, pagination, selected tab) so links and reloads reproduce the same view.
- Keep ephemeral UI state (open/closed dropdown, hover state) local to components with `useState`.
- Use pending navigation state (`useNavigation()`, `fetcher.state`) to show optimistic or loading UI instead of a separate global spinner store.
- Avoid global state (Redux/Zustand/Context) for data that belongs to route loaders — it duplicates the router's own cache and goes stale.

## TypeScript and Testing

- Type loader and action return values; use the generated `Route.LoaderArgs`/`Route.ActionArgs` types (from `./+types/<route>`) for full inference of params and return types.
- Add tests for route loaders, actions, validation failures, and error boundaries as isolated units, independent of full component rendering.
- Use integration tests (e.g. Playwright, Testing Library with a router harness) for critical form and navigation flows.
- Mock network and persistence at the route-service boundary (`app/services/*.server.ts`) rather than mocking `fetch` deep inside components.

## Common Mistakes

- Duplicating loader fetches in `useEffect` — this causes a loading waterfall and defeats the purpose of route-level data loading.
- Mutating data directly inside a loader — loaders can run multiple times (revalidation, prefetch) and must stay read-only.
- Hiding route errors behind a single generic app-level catch-all `ErrorBoundary` instead of route-scoped boundaries that preserve navigation and layout.
- Putting auth checks only in components (e.g. redirecting from a `useEffect`) when the underlying loader data is protected — check and redirect inside the loader itself so no protected data is ever fetched or flashed.

Wersje

RepozytoriumWynikLicencja
Ta strona · Mindrally/skills100Apache-2.0
existential-birds/beagle100Apache-2.0

Migawka bazy z 13.11.2026 · wersja paczki 20928162c455 (skrót SHA-256 całej paczki)

Pobrania w ostatnich 30 dniach: 0 · ranking

Zgłoś problem z tym skillem: szkodliwa treść, błędny opis albo prośba autora o usunięcie.

Podobne skille

  • 100remix
    Opisuje pracę z Remix i React Router v7: zagnieżdżone trasy, loadery i akcje, progresywne ulepszanie przez Form, defer ze strumieniowaniem, ErrorBoundary, useFetcher i integrację z Vite.
  • 100remix-expert
    Buduje i refaktoryzuje moduły tras w React Router 7 i Remix v2: loadery, akcje, Form, stany oczekiwania, zagnieżdżony routing i obsługa błędów, zastępując zbędne pobieranie danych w useEffect.
  • 100remix
    Obsługuje framework full-stack React Remix: loadery, akcje, zagnieżdżony routing i progressive enhancement podczas budowy aplikacji Remix.
  • 100remix
    Pomaga budować aplikacje full-stack React w Remix v2 i jego następcy React Router framework mode: zagnieżdżony routing, loadery i akcje, progressive enhancement, wdrożenia oraz migracja do React Router v7.
  • 100react-router-data-mode
    Buduje aplikacje React w trybie danych React Router: trasy, loadery, akcje, Form, useFetcher oraz interfejs w stanie oczekiwania i optymistyczny.
  • 100react-router-framework-mode
    Buduje pełnostackowe aplikacje React Router w trybie framework: trasy, loadery, akcje, formularze, granice błędów i plik konfiguracyjny.

Uwagi i zgłoszenia

Wiadomość trafi do Bartosza Sokolińskiego. Adres e-mail jest potrzebny tylko do odpowiedzi. Szczegóły w polityce prywatności.