← Public packages
@kody/example-habit-log
A small habit log app built from the app starter: indigo theme, mobile layout, sign-in, and saved entries.
Files
import {
Link,
Outlet,
createRootRouteWithContext,
useRouter,
useRouterState,
} from '@tanstack/react-router'
import { QueryClientProvider } from '@tanstack/react-query'
import { site } from '../content/site.ts'
import { Alert, Button } from '../components/ui.tsx'
import { Document } from '../document.tsx'
import type { RouterContext } from '../router-context.ts'
function Shell(props: { children: React.ReactNode }) {
const router = useRouter()
const context = router.options.context as RouterContext
const title = useRouterState({
select: (state) =>
(state.matches.at(-1)?.staticData as { title?: string } | undefined)
?.title,
})
return (
<QueryClientProvider client={context.queryClient}>
<Document
context={context}
title={title ? `${title} · ${site.name}` : site.name}
>
<a className="skip-link" href="#main">
Skip to content
</a>
<div className="shell">
<header className="header">
<Link to="/" className="brand">
{site.name}
</Link>
<nav className="nav" aria-label="Main">
<Link to="/" activeOptions={{ exact: true }}>
Home
</Link>
<Link to="/items">List</Link>
<Link to="/account">Account</Link>
</nav>
</header>
<main id="main" tabIndex={-1}>
{props.children}
</main>
</div>
</Document>
</QueryClientProvider>
)
}
export const rootRoute = createRootRouteWithContext<RouterContext>()({
component: () => (
<Shell>
<Outlet />
</Shell>
),
errorComponent: function RootError({ error }) {
const router = useRouter()
return (
<Shell>
<section className="card">
<Alert title="Something went wrong">
<p className="muted">{error instanceof Error ? error.message : String(error)}</p>
</Alert>
<div>
<Button onClick={() => void router.invalidate()}>Try again</Button>
</div>
</section>
</Shell>
)
},
notFoundComponent: () => (
<section className="card">
<h1>Page not found</h1>
<p className="muted">
<Link to="/">Go home</Link>
</p>
</section>
),
})