Sometimes a part of an address may or may not be there: /hello and /hello/Asha should both open the same greeting page. In TanStack Start, an optional path param, written {-$name} in the route file’s name, makes one route answer both.
This post builds a greeting page that says hello to the name in the address, or to a friend when there’s no name.
Syntax
// src/routes/<folder>/{-$<param>}.tsx
export const Route = createFileRoute('/<folder>/{-$<param>}')({
component: <Component>,
})
function <Component>() {
const { <param> } = Route.useParams()
// <param> is undefined when the address leaves it out
}
{-$<param>}: curly braces with a-before the$make the path param optional. The route matches with or without that part of the address.Route.useParams(): reads the param. Its value isundefinedwhen the address leaves it out.<Component>: the React component that shows the page.
See “Optional Path Parameters” in TanStack Router’s Path Params guide (opens in a new tab).
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:
import { createFileRoute } from '@tanstack/react-router'
export const Route = createFileRoute('/hello/{-$name}')({
component: Hello,
})
function Hello() {
const { name } = Route.useParams()
if (name === undefined) {
return <h1>Hello, friend!</h1>
}
return <h1>Hello, {name}!</h1>
}
- The file’s name gives the route the path
/hello/{-$name}, which matches both/helloand/hello/Asha. if (name === undefined)checks whether the address had a name, and the page greets a friend when it didn’t.
Step 2: Open Both Addresses
Start the dev server with npm run dev, then open http://localhost:3000/hello:

Now open http://localhost:3000/hello/Nadia:

Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Naming the file $name.tsx | The name is required, so /hello on its own doesn’t match | Name it {-$name}.tsx |
| Showing the value without checking it | When the address has no name, the page has no value to show | Check for undefined first, as in Step 1 |
FAQ
How do I link to the page without a name? Give the param the value undefined: <Link to="/hello/{-$name}" params={{ name: undefined }}> links to /hello (Path Params (opens in a new tab), “Navigating with Optional Parameters”).
Can a route have more than one optional param? Yes. The docs’ example /posts/{-$category}/{-$slug} matches /posts, /posts/tech and /posts/tech/hello-world.
Conclusion
Write {-$name} instead of $name, and one route answers with or without that part of the address. Check the value for undefined before you use it.
Additional Resources
- Path Params (opens in a new tab) in the TanStack Router docs, with “Optional Path Parameters”
- Routing Concepts (opens in a new tab)
Related
- TanStack Start Tutorial — Part 4: Dynamic Routes and Path Params
- TanStack Start Tutorial for Beginners, every part of the series
TanStack, React and other names mentioned are trademarks of their respective owners. JD Bots isn’t affiliated with or endorsed by TanStack.
0 comments