Skip to main content
0 reactions

How to Link to a Route with Path Params in TanStack Start

Card titled Link with Path Params, labelled TanStack Start routing, with to equals /hello/$name above two rows: name Asha to /hello/Asha and name Ben to /hello/Ben.
How to Link to a Route with Path Params in TanStack Start

To link to a page whose address has a path param, such as /hello/Asha, give TanStack Start’s Link two things: the route’s path in to, with its $ part left in, and the value in params. TypeScript then makes sure no link forgets the value.

This post adds two links to a home page, each opening a greeting page with a different name.

Syntax

SyntaxTypeScript
import { Link } from '@tanstack/react-router'

<Link to="/<folder>/$<param>" params={{ <param>: <value> }}>
  <text>
</Link>
  • to: the route’s path, exactly as its route file declares it, with $<param> left in.
  • params: an object with a value for every $ part of the path. The values are text.
  • <text>: what visitors click.

See “Navigating with Path Params” in TanStack Router’s Path Params guide (opens in a new tab), and the Link reference (opens in a new tab).

The example is a small TanStack Start app, set up as in How to Build a TanStack Start App from Scratch Without the CLI, with one route that has a path param. Its file, src/routes/hello/$name.tsx, greets the name in the address:

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>
}

Open the home page, src/routes/index.tsx. Add Link to the import from @tanstack/react-router, and add one link for each name:

src/routes/index.tsxTypeScript
import { createFileRoute, Link } from '@tanstack/react-router'

export const Route = createFileRoute('/')({
  component: Home,
})

function Home() {
  return (
    <main>
      <h1>Hello from TanStack Start</h1>
      <p>
        <Link to="/hello/$name" params={{ name: 'Asha' }}>
          Say hello to Asha
        </Link>
      </p>
      <p>
        <Link to="/hello/$name" params={{ name: 'Ben' }}>
          Say hello to Ben
        </Link>
      </p>
    </main>
  )
}

Both links point to the same route. params={{ name: 'Asha' }} fills in $name, so the first link opens /hello/Asha and the second /hello/Ben.

Start the dev server with npm run dev and open http://localhost:3000:

A home page with the heading Hello from TanStack Start and two links, Say hello to Asha and Say hello to Ben.
Two Link components to the same route, with different params.

Click Say hello to Ben:

A browser page that shows the heading Hello, Ben! after clicking the link Say hello to Ben.
Clicking Say hello to Ben opens /hello/Ben.

Step 3: Let TypeScript Catch a Missing Param

Every link to this route needs params. If one doesn’t have it, for example <Link to="/hello/$name">, the type check fails. Run it in your project folder. --noEmit makes it check the types without writing any files:

TerminalBash
npx tsc --noEmit
Output (the lines that matter)Plain text
src/routes/index.tsx(17,10): error TS2741: Property 'params' is missing in type '{ to: "/hello/$name"; children: string; }' but required in type 'MakeRequiredPathParams<RouterCore<Route<Register, any, "/", "/", string, "__root__", undefined, {}, {}, AnyContext, AnyContext, {}, undefined, RootRouteChildren, FileRouteTypes, unknown, unknown, undefined>, "never", false, RouterHistory, Record<...>>, string, "/hello/$name">'.

The error says that params is missing on line 17, the link to Ben. Add params back, and the error goes away.

Keep --noEmit unless your tsconfig.json sets noEmit: without either, tsc writes a .js copy of every file, and the dev server then reports conflicting routes. Fixed: “Conflicting configuration paths were found for the following routes” in TanStack Start shows the fix.

Common Mistakes

MistakeWhat happensDo this instead
Writing the finished address in to, such as to="/hello/Ben"The type check fails, because to takes only the paths your routes declareUse to="/hello/$name" with params={{ name: 'Ben' }}
Leaving out paramserror TS2741: Property 'params' is missingAdd params with a value for every $ in the path

FAQ

What does the link look like in the page? A normal <a> tag with the finished address, such as /hello/Ben, so it also works in a new tab.

Can one link fill in two params? Yes. Give params a value for each, such as params={{ authorId: '7', bookId: '2' }} for a route with the path /authors/$authorId/books/$bookId.

Conclusion

A link to a route with a path param takes the route’s path in to and the values in params. TypeScript checks both, so a link can’t point to a route that doesn’t exist or forget a value.

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