File-based routing makes it easy to create and organize pages by matching files to URLs automatically. This means you don't have to write extra code to connect pages and links.
0
0
Why file-based routing simplifies navigation in Remix
Introduction
When building a website with many pages that need clear navigation.
When you want to quickly add new pages without changing routing code.
When you want your folder structure to reflect your website's URL structure.
When you want to avoid mistakes from manually writing route paths.
When you want to keep your project organized and easy to understand.
Syntax
Remix
src/routes/ ├── index.tsx // maps to '/' ├── about.tsx // maps to '/about' ├── blog/ │ ├── index.tsx // maps to '/blog' │ └── $postId.tsx // maps to '/blog/:postId'
Each file or folder inside src/routes automatically becomes a route.
Dynamic routes use $ before the file name to capture URL parameters.
Examples
The
index.tsx file inside routes folder maps to the root URL.Remix
src/routes/index.tsx
// This file creates the homepage at '/'A file named
about.tsx creates a page at /about automatically.Remix
src/routes/about.tsx
// This file creates the '/about' pageThe
$postId.tsx file captures the part after /blog/ as a parameter named postId.Remix
src/routes/blog/$postId.tsx // This file creates dynamic routes like '/blog/123' or '/blog/hello-world'
Sample Program
This example shows a dynamic route component that reads the URL parameter postId and displays it on the page.
Remix
import { useParams } from '@remix-run/react'; export default function BlogPost() { const params = useParams(); return ( <main> <h1>Blog Post</h1> <p>You are reading post ID: {params.postId}</p> </main> ); } // This component is saved as src/routes/blog/$postId.tsx
OutputSuccess
Important Notes
File names and folder structure directly control your website's URLs, so organizing files well helps keep navigation clear.
Dynamic routes let you handle many similar pages with one file, like blog posts or user profiles.
Remix automatically handles linking and loading pages based on these files, saving you time and reducing errors.
Summary
File-based routing links your files to URLs automatically.
It makes adding and organizing pages simple and clear.
Dynamic routes let you create flexible URLs easily.