Implements React Router v7 app structure, routing patterns, and component templates. Use when creating or modifying React Router v7 applications to ensure consistent folder structure, data loading patterns, and component architecture.
app/routes/.route.tsx for route components.users.$id/route.tsx when a route needs colocated helpers, components, or tests; sibling files in the folder do not become routes.app/routes/, configure flatRoutes({ ignoredRouteFiles: [...] }) to exclude *.test.* and *.spec.* from route discovery._app, _landing.users.$id.tsx.loader functions for server-side data fetching.action functions for form submissions and mutations.useLoaderData() and useActionData() hooks.useNavigation().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;
ErrorBoundary components for route-level error handling.isRouteErrorResponse() for typed error handling.meta function for dynamic page metadata.headers function for custom HTTP headers.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.components folder.app/components and route-scoped components directories.app-header.tsx, empty-state.tsx, todo-form.tsx.app/components
|
+-- ui # shadcn/ui components
| |
| +-- button.tsx
| ...
|
+-- app-header.tsx # Shared across multiple routes
+-- empty-state.tsx # Shared across multiple features
|
Use collocation to group files related to layouts and pages.
route.tsx and colocate route-local tests as route.test.ts only when route discovery is configured to ignore test/spec files.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
Use nested routes to split complex pages (e.g., tabs) into independent route modules.
<Outlet />.loader) and mutations (action).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.Manage modal state via URLs instead of useState.
<Outlet />.<Outlet /> (often using a Dialog component).<Link to="..">).Example:
routes/users.tsx: Lists users and contains <Outlet />.routes/users.new.tsx: Renders the "Create User" modal.