react-router-v7-app
React Router v7 App Implementation
1. Route Structure
- Use file-based routing in
app/routes/. - Route files:
route.tsxfor route components. - Use underscore prefix for route groups:
_app,_landing. - Use dot notation for nested routes:
users.$id.tsx.
2. Data Loading Patterns
- Use
loaderfunctions for server-side data fetching. - Use
actionfunctions for form submissions and mutations. - Leverage
useLoaderData()anduseActionData()hooks. - Handle loading states with
useNavigation().
3. Route Component Structure
Use arrow functions for all exports.
import type { Route } from './+types/route';
export const loader = async ({ request, params, context }: Route.LoaderArgs) => {
// Server-side data loading
// Return an object
return { ... };
};
export const action = async ({ request, params, context }: Route.ActionArgs) => {
// Handle form submissions and mutations
// Return an object
return { ... };
};
const Page = ({ loaderData, actionData }: Route.ComponentProps) => {
// Component implementation with typed props
return (
<div>
{/* Content */}
</div>
);
};
export default Page;
4. Error Handling
- Use
ErrorBoundarycomponents for route-level error handling. - Implement proper error responses in loaders/actions.
- Use
isRouteErrorResponse()for typed error handling.
5. Meta and Headers
- Export
metafunction for dynamic page metadata. - Export
headersfunction for custom HTTP headers. - Use proper SEO practices with meta tags.
6. Project Structure
The app folder should follow this structure:
public # Static files (images, fonts, etc.)
|
app
|
+-- components # Shared components
|
+-- config # Global configuration and environment variables
|
+-- constants # Global constants
|
+-- hooks # Shared hooks
|
+-- lib # Pre-configured reusable libraries
|
+-- routes # React Router routing directory
|
+-- stores # Global state stores
|
+-- testing # Test utilities and mock servers
|
+-- types # Base types used across the application
|
+-- utils # Shared utility functions
|
app/components
app/components
|
+-- shadcn
| |
| +-- ui # shadcn/ui components
| |
| +-- button.tsx
| ...
|
routes
Use collocation to group files related to layouts and pages.
app/routes/_app._index
|
+-- assets # Feature-specific static files
|
+-- components # Feature-scoped components
|
+-- constants # Feature-specific constants
|
+-- hooks # Feature-scoped hooks
|
+-- services # Feature services (business logic)
|
+-- stores # Feature state stores
|
+-- types # Feature-specific TypeScript types
|
+-- utils # Feature-specific utility functions
Example structure:
app/routes
|
+-- _app # App-wide layout & shared files
| |
| +-- route.tsx # Layout file
|
+-- _app.index # App top page
| |
| +-- route.tsx # Page file
|
+-- _app.todos # (Example) todos layout & shared files
| |
| +-- components # Components used in layout
| |
| +-- types # Types common to todos
| |
| +-- route.tsx # Layout file
|
+-- _app.todos._index # (Example) todos top page
| |
| +-- components # Components used in page
| |
| +-- route.tsx # Page file
|
+-- _app.todos.$todoId.edit._index # (Example) todos edit page
| |
| +-- components # Components used in page
| |
| +-- hooks # Hooks used in page
| |
| +-- stores # Stores used in page
| |
| +-- route.tsx # Page file
|
+-- _app.todos.$todoId_.delete # (Example) todos delete resource route
|
+-- route.tsx # Resource route file
7. Advanced Routing Patterns
Nested Routes for UI Segmentation
Use nested routes to split complex pages (e.g., tabs) into independent route modules.
- Parent route renders shared UI and
<Outlet />. - Child routes handle their own data loading (
loader) and mutations (action). - Reduces prop drilling and isolates logic.
Example (Tabs):
routes/user.tsx: Renders tabs navigation and<Outlet />.routes/user.profile.tsx: Renders profile tab content.routes/user.account.tsx: Renders account tab content.
Route-based Modals
Manage modal state via URLs instead of useState.
- Define the modal as a child route.
- Parent route renders the background UI and
<Outlet />. - Modal component renders inside the
<Outlet />(often using a Dialog component). - Closing the modal is a navigation action (e.g.,
<Link to="..">).
Example:
routes/users.tsx: Lists users and contains<Outlet />.routes/users.new.tsx: Renders the "Create User" modal.
More from atman-33/skills
dnd-kit-implementation
Guide for implementing sortable and droppable components using dnd-kit library. Use this skill when building React applications that require drag-and-drop functionality with both container reordering (useSortable) and item dropping (useDroppable) capabilities, such as Kanban boards, file management systems, or playlist editors.
38tech-article-humanizer
Transform technical article drafts or source materials into human-like, high-quality Japanese technical articles. Use this skill when the user wants to generate, rewrite, or humanize technical articles (especially about TypeScript, JavaScript, React, or frontend topics) following specific human-writing patterns and style guidelines. Triggers include requests like "記事を人間風に", "tech article を生成", "humanize this article", or providing article source materials.
4agent-memory
Use this skill when the user asks to save, remember, recall, or organize memories. Triggers on phrases like 'remember this', 'save this', 'note this', 'what did we discuss about...', 'check your notes', 'clean up memories'. Also use proactively when discovering valuable findings worth preserving.
2pr-assistant
Analyzes git changes and assists with creating comprehensive pull requests. Use when user wants to create a PR, review changes before PR, or needs help drafting PR descriptions. Triggers on phrases like 'create PR', 'make a pull request', 'draft PR description', 'what changed in this branch', 'prepare PR'.
1openspec-bulk-archive-change
Archive multiple completed changes at once. Use when archiving several parallel changes.
1serena-skills
Standalone Serena MCP tools for code intelligence - symbol search, file ops, memory, editing, config, workflow helpers, and shell execution without MCP server
1