Documentation
Framework
Version

React Example: Authenticated Routes

import { createFileRoute } from '@tanstack/react-router'
import * as React from 'react'

import { fetchInvoiceById } from '../posts'

export const Route = createFileRoute('/_auth/invoices/$invoiceId')({
  loader: async ({ params: { invoiceId } }) => {
    return {
      invoice: await fetchInvoiceById(parseInt(invoiceId)),
    }
  },
  component: InvoicePage,
})

function InvoicePage() {
  const { invoice } = Route.useLoaderData()

  return (
    <section className="grid gap-2">
      <h2 className="text-lg">
        <strong>Invoice No.</strong> #{invoice.id.toString().padStart(2, '0')}
      </h2>
      <p>
        <strong>Invoice title:</strong> {invoice.title}
      </p>
      <p>
        <strong>Invoice body:</strong> {invoice.body}
      </p>
    </section>
  )
}
import { createFileRoute } from '@tanstack/react-router'
import * as React from 'react'

import { fetchInvoiceById } from '../posts'

export const Route = createFileRoute('/_auth/invoices/$invoiceId')({
  loader: async ({ params: { invoiceId } }) => {
    return {
      invoice: await fetchInvoiceById(parseInt(invoiceId)),
    }
  },
  component: InvoicePage,
})

function InvoicePage() {
  const { invoice } = Route.useLoaderData()

  return (
    <section className="grid gap-2">
      <h2 className="text-lg">
        <strong>Invoice No.</strong> #{invoice.id.toString().padStart(2, '0')}
      </h2>
      <p>
        <strong>Invoice title:</strong> {invoice.title}
      </p>
      <p>
        <strong>Invoice body:</strong> {invoice.body}
      </p>
    </section>
  )
}
Our Partners
Code Rabbit
Cloudflare
Netlify
Neon
Clerk
Convex
Sentry
Strapi
Subscribe to Bytes

Your weekly dose of JavaScript news. Delivered every Monday to over 100,000 devs, for free.

Bytes

No spam. Unsubscribe at any time.

Subscribe to Bytes

Your weekly dose of JavaScript news. Delivered every Monday to over 100,000 devs, for free.

Bytes

No spam. Unsubscribe at any time.