Most websites have pages that look the same but show different data, such as one page for every book in a shop. In TanStack Start, you don’t write a file for each of them. One route file with a $ in its name makes a dynamic route: it matches every address of that shape, and the part that changes arrives in your code as a path param.
This is Part 4 of the TanStack Start Tutorial, a free, hands-on course for complete beginners. You continue the Bookshop from Part 3 and give every book its own page.
By the end of this part, you’ll have:
- a page for every book, at
/books/1,/books/2and so on, made from one file - a Books page that links each book to its page
- a real 404 page, with the status code 404, for a book ID that doesn’t exist
This is Part 4 of the TanStack Start Tutorial. The parts in Module B, Routing:
- Part 4: Dynamic Routes and Path Params (you are here)
- Part 5: Search Params: Type-Safe Filters and Sorting
- Part 6: Layouts, Nested Routes and Navigation in Code
All parts of the TanStack Start Tutorial
Module A: Start Here
- Part 1: What Is TanStack Start? A Full-Stack React Framework Explained
- Part 2: Create Your First TanStack Start App
- Part 3: Pages and Links: File-Based Routing
Module B: Routing
- Part 4: Dynamic Routes and Path Params
- Part 5: Search Params: Type-Safe Filters and Sorting
- Part 6: Layouts, Nested Routes and Navigation in Code
Module C: Data
- Part 7: Load Data with Route Loaders
- Part 8: Server Functions
- Part 9: Forms and Mutations
- Part 10: TanStack Query with TanStack Start
- Part 11: Streaming and Deferred Data
Module D: Server
- Part 12: What Runs Where: the Execution Model
- Part 13: Environment Variables and Secrets
- Part 14: Server Routes: Build a JSON API
- Part 15: Middleware and Context
- Part 16: Errors and Not Found Pages
Module E: Database
- Part 17: Connect PostgreSQL with Drizzle ORM
- Part 18: Create, Edit and Delete Data
Module F: Sign-In
- Part 19: Sessions and Cookies
- Part 20: Protected Routes
- Part 21: Sign-In with Better Auth: Email and GitHub
Module G: Rendering
- Part 22: Server-Side Rendering and Hydration Errors
- Part 23: Selective SSR and SPA Mode
- Part 24: Static Prerendering and ISR
Module H: Look and SEO
- Part 25: Style with Tailwind CSS
- Part 26: SEO: Titles, Meta Tags, Sitemap and Structured Data
Module I: Ship
- Part 27: Production Build and Checklist
- Part 28: Deploy to Netlify for Free with a Neon Database
- Part 29: Final Project, Cheat Sheet and What to Learn Next
Cost: free. Everything in this part runs on your own computer.
Every command and output on this page is real, run with TanStack Start 1 on Node.js 26.
Prerequisites
- The Bookshop app from Part 3: Pages and Links: File-Based Routing.
Where Your Project Is Now
At the end of Part 3, the Bookshop has three pages, Home, Books and About, a header that links them, and a page for addresses that don’t exist. The Books page lists four books as plain text.
If you’re starting with this part, follow Part 2 and Part 3 first, then make sure src/routes/books/index.tsx matches this file:
import { createFileRoute } from "@tanstack/react-router";
export const Route = createFileRoute("/books/")({ component: Books });
function Books() {
return (
<main className="mx-auto max-w-2xl p-8">
<h1 className="text-4xl font-bold">Books</h1>
<ul className="mt-4 list-disc pl-6 text-lg">
<li>The Paper Lantern Café by Mira Kale</li>
<li>A Map of Quiet Places by Tomas Vell</li>
<li>The Clockmaker’s Garden by Mira Kale</li>
<li>Salt and Starlight by Nadia Quill</li>
</ul>
</main>
);
}
Step 1: Add a Dynamic Route for the Book Pages
Start the dev server from the bookshop folder, if it isn’t already running:
npm run dev
In src/routes/books, the folder of the Books page, create a file named $bookId.tsx. The $ at the start of the name makes this a dynamic route: the part of the address after /books/ can be any value, and the route gets that value under the name bookId. A value like this is called a path param.
As in Part 3, the dev server fills the new file with starter code, which shows Hello "/books/$bookId"!. Replace all of it with this:
import { createFileRoute } from "@tanstack/react-router";
export const Route = createFileRoute("/books/$bookId")({ component: BookPage });
function BookPage() {
const { bookId } = Route.useParams();
return (
<main className="mx-auto max-w-2xl p-8">
<h1 className="text-4xl font-bold">Book {bookId}</h1>
</main>
);
}
What’s new here:
createFileRoute("/books/$bookId")holds the route’s path, which TanStack Start writes for you from the file’s place and name.$bookIdstays in it as it is, because it stands for any value.Route.useParams()gives the component the route’s path params. For the address/books/2, that’s{ bookId: "2" }, andconst { bookId } = …takes the value out so the page can show it.
Open http://localhost:3000/books/2:

The page shows the value that follows /books/ in the address, so this one file answers /books/1, /books/2 and every other book ID. Books stays bold in the header, because the book pages sit under the Books page’s address, as Part 3 said it would.
Step 2: Keep the Book List in One File
The book page needs each book’s title and author, and so does the Books page. Instead of typing the list twice, keep it in one file that both pages use. Create a folder named lib in src, and in it a file named books.ts:
export const books = [
{ id: "1", title: "The Paper Lantern Café", author: "Mira Kale" },
{ id: "2", title: "A Map of Quiet Places", author: "Tomas Vell" },
{ id: "3", title: "The Clockmaker’s Garden", author: "Mira Kale" },
{ id: "4", title: "Salt and Starlight", author: "Nadia Quill" },
];
exportmakesbooksavailable to other files, which bring it in withimport.- Each book has an
id, the value its address uses. The IDs are text, in quotes, because a path param is always text: for/books/2,bookIdis"2", not the number2. - The file ends in
.ts, not.tsx, because it holds data and no page markup.
Advertisement
Step 3: Link Every Book to Its Page
Now turn the plain list on the Books page into links. Replace src/routes/books/index.tsx with this:
import { createFileRoute, Link } from "@tanstack/react-router";
import { books } from "#/lib/books";
export const Route = createFileRoute("/books/")({ component: Books });
function Books() {
return (
<main className="mx-auto max-w-2xl p-8">
<h1 className="text-4xl font-bold">Books</h1>
<ul className="mt-4 list-disc pl-6 text-lg">
{books.map((book) => (
<li key={book.id}>
<Link
to="/books/$bookId"
params={{ bookId: book.id }}
className="underline"
>
{book.title} by {book.author}
</Link>
</li>
))}
</ul>
</main>
);
}
What changed:
import { books } from "#/lib/books"brings in the list from Step 2.#/is the app’s shortcut for thesrcfolder (How to Use Path Aliases in TanStack Start).books.map(…)makes one list item for every book.key={book.id}gives each item its own name, which React needs to tell the items apart (React’s guide to lists (opens in a new tab)).to="/books/$bookId"is the path of the route from Step 1, with$bookIdleft in, andparams={{ bookId: book.id }}fills it in. For the second book, the link opens/books/2.
TypeScript checks the pair: leave out params, and npx tsc reports an error (see Common Mistakes, below).
Open http://localhost:3000/books:

Click A Map of Quiet Places by Tomas Vell: the page for book 2 opens, still showing Book 2.
Step 4: Show the Book, or a 404 for an Unknown ID
The book page should show the book’s title and author. A book ID that doesn’t exist, such as 99, should get the 404 page from Part 3. Replace src/routes/books/$bookId.tsx with this:
import { createFileRoute, notFound } from "@tanstack/react-router";
import { books } from "#/lib/books";
export const Route = createFileRoute("/books/$bookId")({
loader: ({ params }) => {
const book = books.find((book) => book.id === params.bookId);
if (book === undefined) {
throw notFound();
}
return book;
},
component: BookPage,
});
function BookPage() {
const book = Route.useLoaderData();
return (
<main className="mx-auto max-w-2xl p-8">
<h1 className="text-4xl font-bold">{book.title}</h1>
<p className="mt-4 text-lg">by {book.author}</p>
</main>
);
}
What each part does:
loaderis a function that TanStack Start runs before it shows the page. What it returns becomes the page’s data, and it gets the path params asparams.books.find(…)looks for the book whoseidequalsparams.bookId, and givesundefinedwhen no book matches.throw notFound()stops the loader and shows the not-found page: thenotFoundComponentyou added to the root route in Part 3. The server answers with the status code 404, which tells browsers and search engines that the page doesn’t exist.Route.useLoaderData()gives the component what the loader returned: the book.
Throw notFound() from the loader, not from the component: thrown from the component, the page looks the same, but the server answers with the status code 200 and logs an error. Fixed: “Error in renderToReadableStream: { isNotFound: true }” in TanStack Start explains why.
Open http://localhost:3000/books/2:

Then open http://localhost:3000/books/99, an ID that no book has:

The header and its bold Books link stay, and the server answers with the status code 404.
Clean Up
Stop the dev server: click in the terminal and press Ctrl+C. Keep the bookshop folder, because Part 5 continues from it.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Writing the address itself in to, such as to="/books/2" | npx tsc reports error TS2820: Type '"/books/2"' is not assignable to type '"." | ".." | "/" | "/about" | "/books" | "/books/$bookId"'. Did you mean '"/books"'? | Use to="/books/$bookId" with params={{ bookId: "2" }} |
Leaving out params | npx tsc reports error TS2741: Property 'params' is missing | Add params with a value for every $ in the path |
Writing the IDs as numbers, such as id: 2 | npx tsc reports error TS2367: This comparison appears to be unintentional because the types 'number' and 'string' have no overlap. | Write the IDs as text: id: "2" |
Naming the file bookId.tsx, without the $ | The route matches only the address /books/bookId | Start the file name with $ |
Troubleshooting
| Problem | Fix |
|---|---|
The book page shows Hello "/books/$bookId"! | That’s the starter code the dev server wrote into the new file. Replace it with the code from Step 1. |
| Every book page shows Page not found | Check that the IDs in src/lib/books.ts are in quotes. A number never equals the text in params.bookId. |
The terminal prints Error in renderToReadableStream: { isNotFound: true } | Throw notFound() from the loader, as in Step 4, not from the component. |
FAQ
Why is the file named $bookId.tsx? The $ marks the part of the address that changes, and the rest of the name is the name its value gets in params. A file named $slug.tsx would give you params.slug.
Why is params.bookId text and not a number? A path param comes from the address, and an address is text, so /books/2 gives "2". That’s why the book IDs in books.ts are text too.
Can one route have more than one path param? Yes. Each $ part of a path is its own param, as in /authors/$authorId/books/$bookId, and child routes can read the params of the routes above them (Path Params (opens in a new tab) in the TanStack Router docs).
Conclusion
One file, $bookId.tsx, now gives every book its own page. The $ made it a dynamic route, Route.useParams() and the loader’s params read the path param, Link fills it in with params, and notFound() sends unknown IDs to a real 404 page.
← Previous: Part 3: Pages and Links: File-Based Routing
Coming next: Part 5: Search Params: Type-Safe Filters and Sorting.
Additional Resources
- Path Params (opens in a new tab) in the TanStack Router docs
- Routing Concepts (opens in a new tab), with dynamic route segments
- Navigation (opens in a new tab), with linking to a route that has path params
- Data Loading (opens in a new tab), with route loaders
- Not Found Errors (opens in a new tab), with
notFound()
TanStack, React and other names mentioned are trademarks of their respective owners. JD Bots isn’t affiliated with or endorsed by TanStack.
0 comments