Skip to main content
0 reactions

How to Use Path Params in TanStack Start

Card titled Path Params, labelled TanStack Start routing, with the route /hello/$name above two rows: /hello/Asha to Hello, Asha! and /hello/Ben to Hello, Ben!
How to Use Path Params in TanStack Start

A path param is the part of a web address that changes from page to page, such as the name in /hello/Asha. In TanStack Start, you make one by putting a $ in a route file’s name, and you read its value with Route.useParams().

This post builds a greeting page that says hello to whatever name is in the address.

Syntax

SyntaxTypeScript
// src/routes/<folder>/$<param>.tsx
export const Route = createFileRoute('/<folder>/$<param>')({
  component: <Component>,
})

function <Component>() {
  const { <param> } = Route.useParams()
  // use <param> in the page
}
  • $<param> in the file name: the $ makes that part of the address a path param, and <param> is the name its value gets. It matches the text up to the next /.
  • '/<folder>/$<param>': the route’s path. TanStack Start writes it for you from the file’s place and name.
  • Route.useParams(): gives the component an object with the route’s path params, such as { name: 'Asha' }. The values are always text.
  • <Component>: the React component that shows the page.

TanStack Router’s Path Params guide (opens in a new tab) describes every option.

The example is a small TanStack Start app with only a home page, set up as in How to Build a TanStack Start App from Scratch Without the CLI. Any TanStack Start app works the same way.

Step 1: Create the Route File

In src/routes, create a folder named hello, and in it a file named $name.tsx:

src/routes/hello/$name.tsxTypeScript
import { createFileRoute } from '@tanstack/react-router'

export const Route = createFileRoute('/hello/$name')({
  component: Hello,
})

function Hello() {
  const { name } = Route.useParams()
  return <h1>Hello, {name}!</h1>
}
  • The file’s place and name give the route the path /hello/$name: /hello/ followed by any name.
  • const { name } = Route.useParams() reads the param, and the heading shows it.

Step 2: Open the Page

Start the dev server with npm run dev, then open http://localhost:3000/hello/Asha:

A browser page that shows only the heading Hello, Asha! in large bold text.
The page at /hello/Asha reads Asha from the address.

Change the name in the address, and the greeting changes with it. Characters that an address can’t hold as they are arrive decoded: http://localhost:3000/hello/Mira%20Kale, where %20 stands for a space, shows this:

A browser page that shows the heading Hello, Mira Kale! for an address with a space in the name.
A space in the address arrives as a normal space.

Common Mistakes

MistakeWhat happensDo this instead
Naming the file name.tsx, without the $The route matches only the address /hello/nameStart the file name with $
Treating the value as a number, as in /items/2The value is the text "2"Compare it with text, or turn it into a number with Number()
Expecting one param to hold a slash, as in /hello/Mira/KaleThe route doesn’t match, because a path param stops at the next /Use one path param for each part of the address

FAQ

Can a loader read the param? Yes. A route’s loader gets the same values as params, such as params.name (Path Params (opens in a new tab), “Path Params in Loaders”).

Can a route have more than one path param? Yes. Each $ part of the path is its own param, so a route with the path /authors/$authorId/books/$bookId gets both authorId and bookId.

Conclusion

A $ in a route file’s name makes a path param, and Route.useParams() reads its value. One file then answers every address of that shape.

Additional Resources

TanStack, React and other names mentioned are trademarks of their respective owners. JD Bots isn’t affiliated with or endorsed by TanStack.

0 comments