Skip to main content
0 reactions

How to Use Optional Path Params in TanStack Start

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

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

SyntaxTypeScript
// 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 is undefined when 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:

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()

  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 /hello and /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:

A browser page that shows the heading Hello, friend! for the address /hello, which has no name.
At /hello the optional param is missing, so the page greets a friend.

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

A browser page that shows the heading Hello, Nadia! for the address /hello/Nadia.
The same route at /hello/Nadia greets Nadia by name.

Common Mistakes

MistakeWhat happensDo this instead
Naming the file $name.tsxThe name is required, so /hello on its own doesn’t matchName it {-$name}.tsx
Showing the value without checking itWhen the address has no name, the page has no value to showCheck 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

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

0 comments