Open History to see the last commit
1import ReactDOM from 'react-dom/client'
2import {
3 ErrorComponent,
4 Link,
5 Outlet,
6 RouterProvider,
7 createRootRoute,
8 createRoute,
9 createRouter,
10 redirect,
11} from '@tanstack/react-router'
12import { TanStackRouterDevtools } from '@tanstack/react-router-devtools'
13import { NotFoundError, fetchPost, fetchPosts } from './posts'
14import './styles.css'
15import type { ErrorComponentProps } from '@tanstack/react-router'
16β
17const rootRoute = createRootRoute({
18 component: RootComponent,
19 notFoundComponent: () => {
20 return (
21 <div>
22 <p>This is the notFoundComponent configured on root route</p>
23 <Link to="/">Start Over</Link>
24 </div>
25 )
26 },
27})
28β
29function RootComponent() {
30 return (
31 <>
32 <div className="p-2 flex gap-2 text-lg border-b">
33 <Link
34 to="/"
35 activeProps={{
36 className: 'font-bold',
37 }}
38 activeOptions={{ exact: true }}
39 >
40 Home
41 </Link>{' '}
42 <Link
43 to="/posts"
44 activeProps={{
45 className: 'font-bold',
46 }}
47 >
48 Posts
49 </Link>{' '}
50 <Link to="/posts" viewTransition>
51 View Transition
52 </Link>{' '}
53 <Link to="/posts" viewTransition={{ types: ['dummy'] }}>
54 View Transition types
55 </Link>{' '}
56 <Link
57 to="/layout-a"
58 activeProps={{
59 className: 'font-bold',
60 }}
61 >
62 Layout
63 </Link>{' '}
64 <Link
65 // @ts-expect-error
66 to="/this-route-does-not-exist"
67 activeProps={{
68 className: 'font-bold',
69 }}
70 >
71 This Route Does Not Exist
72 </Link>{' '}
73 <div className="flex items-center">
74 <svg width="20" height="20" viewBox="0 0 20 20" role="img">
75 <title id="rectTitle">Link in SVG</title>
76 <Link to="/posts" aria-label="Open posts from SVG">
77 <rect
78 x="0"
79 y="0"
80 width="20"
81 height="20"
82 rx="4"
83 fill="blue"
84 strokeWidth="2"
85 />
86 </Link>
87 </svg>
88 </div>
89 </div>
90 <Outlet />
91 <TanStackRouterDevtools position="bottom-right" />
92 </>
93 )
94}
95const indexRoute = createRoute({
96 getParentRoute: () => rootRoute,
97 path: '/',
98 component: IndexComponent,
99})
100β
101function IndexComponent() {
102 return (
103 <div className="p-2">
104 <h3>Welcome Home!</h3>
105 </div>
106 )
107}
108β
109export const postsRoute = createRoute({
110 getParentRoute: () => rootRoute,
111 path: 'posts',
112 loader: () => fetchPosts(),
113}).lazy(() => import('./posts.lazy').then((d) => d.Route))
114β
115const postsIndexRoute = createRoute({
116 getParentRoute: () => postsRoute,
117 path: '/',
118 component: PostsIndexComponent,
119})
120β
121function PostsIndexComponent() {
122 return <div>Select a post.</div>
123}
124β
125const postRoute = createRoute({
126 getParentRoute: () => postsRoute,
127 path: '$postId',
128 errorComponent: PostErrorComponent,
129 loader: ({ params }) => fetchPost(params.postId),
130 component: PostComponent,
131})
132β
133function PostErrorComponent({ error }: ErrorComponentProps) {
134 if (error instanceof NotFoundError) {
135 return <div>{error.message}</div>
136 }
137β
138 return <ErrorComponent error={error} />
139}
140β
141function PostComponent() {
142 const post = postRoute.useLoaderData()
143β
144 return (
145 <div className="space-y-2">
146 <h4 className="text-xl font-bold">{post.title}</h4>
147 <hr className="opacity-20" />
148 <div className="text-sm">{post.body}</div>
149 </div>
150 )
151}
152β
153const layoutRoute = createRoute({
154 getParentRoute: () => rootRoute,
155 id: '_layout',
156 component: LayoutComponent,
157})
158β
159function LayoutComponent() {
160 return (
161 <div className="p-2">
162 <div className="border-b">I'm a layout</div>
163 <div>
164 <Outlet />
165 </div>
166 </div>
167 )
168}
169β
170const layout2Route = createRoute({
171 getParentRoute: () => layoutRoute,
172 id: '_layout-2',
173 component: Layout2Component,
174})
175β
176function Layout2Component() {
177 return (
178 <div>
179 <div>I'm a nested layout</div>
180 <div className="flex gap-2 border-b">
181 <Link
182 to="/layout-a"
183 activeProps={{
184 className: 'font-bold',
185 }}
186 >
187 Layout A
188 </Link>
189 <Link
190 to="/layout-b"
191 activeProps={{
192 className: 'font-bold',
193 }}
194 >
195 Layout B
196 </Link>
197 </div>
198 <div>
199 <Outlet />
200 </div>
201 </div>
202 )
203}
204β
205const layoutARoute = createRoute({
206 getParentRoute: () => layout2Route,
207 path: '/layout-a',
208 component: LayoutAComponent,
209})
210β
211function LayoutAComponent() {
212 return <div>I'm layout A!</div>
213}
214β
215const layoutBRoute = createRoute({
216 getParentRoute: () => layout2Route,
217 path: '/layout-b',
218 component: LayoutBComponent,
219})
220β
221function LayoutBComponent() {
222 return <div>I'm layout B!</div>
223}
224β
225const paramsPsRoute = createRoute({
226 getParentRoute: () => rootRoute,
227 path: '/params-ps',
228})
229β
230const paramsPsIndexRoute = createRoute({
231 getParentRoute: () => paramsPsRoute,
232 path: '/',
233 component: function ParamsIndex() {
234 return (
235 <div>
236 <h3 className="pb-2">Named path params</h3>
237 <ul className="grid mb-2">
238 <li>
239 <Link
240 data-testid="l-to-named-foo"
241 to="/params-ps/named/$foo"
242 params={{ foo: 'foo' }}
243 >
244 /params-ps/named/$foo
245 </Link>
246 </li>
247 <li>
248 <Link
249 data-testid="l-to-named-prefixfoo"
250 to="/params-ps/named/prefix{$foo}"
251 params={{ foo: 'foo' }}
252 >
253 /params-ps/named/{'prefix{$foo}'}
254 </Link>
255 </li>
256 <li>
257 <Link
258 data-testid="l-to-named-foosuffix"
259 to="/params-ps/named/{$foo}suffix"
260 params={{ foo: 'foo' }}
261 >
262 /params-ps/named/{'{$foo}suffix'}
263 </Link>
264 </li>
265 </ul>
266 <hr />
267 <h3 className="pb-2">Wildcard path params</h3>
268 <ul className="grid mb-2">
269 <li>
270 <Link
271 data-testid="l-to-wildcard-foo"
272 to="/params-ps/wildcard/$"
273 params={{ _splat: 'foo' }}
274 >
275 /params-ps/wildcard/$
276 </Link>
277 </li>
278 <li>
279 <Link
280 data-testid="l-to-wildcard-prefixfoo"
281 to="/params-ps/wildcard/prefix{$}"
282 params={{ _splat: 'foo' }}
283 >
284 /params-ps/wildcard/{'prefix{$}'}
285 </Link>
286 </li>
287 <li>
288 <Link
289 data-testid="l-to-wildcard-foosuffix"
290 to="/params-ps/wildcard/{$}suffix"
291 params={{ _splat: 'foo' }}
292 >
293 /params-ps/wildcard/{'{$}suffix'}
294 </Link>
295 </li>
296 </ul>
297 </div>
298 )
299 },
300})
301β
302const paramsPsNamedRoute = createRoute({
303 getParentRoute: () => paramsPsRoute,
304 path: '/named',
305})
306β
307const paramsPsNamedIndexRoute = createRoute({
308 getParentRoute: () => paramsPsNamedRoute,
309 path: '/',
310 beforeLoad: () => {
311 throw redirect({ to: '/params-ps' })
312 },
313})
314β
315const paramsPsNamedFooRoute = createRoute({
316 getParentRoute: () => paramsPsNamedRoute,
317 path: '/$foo',
318 component: function ParamsNamedFoo() {
319 const p = paramsPsNamedFooRoute.useParams()
320 return (
321 <div>
322 <h3>ParamsNamedFoo</h3>
323 <div data-testid="params-output">{JSON.stringify(p)}</div>
324 </div>
325 )
326 },
327})
328β
329const paramsPsNamedFooPrefixRoute = createRoute({
330 getParentRoute: () => paramsPsNamedRoute,
331 path: '/prefix{$foo}',
332 component: function ParamsNamedFooMarkdown() {
333 const p = paramsPsNamedFooPrefixRoute.useParams()
334 return (
335 <div>
336 <h3>ParamsNamedFooPrefix</h3>
337 <div data-testid="params-output">{JSON.stringify(p)}</div>
338 </div>
339 )
340 },
341})
342β
343const paramsPsNamedFooSuffixRoute = createRoute({
344 getParentRoute: () => paramsPsNamedRoute,
345 path: '/{$foo}suffix',
346 component: function ParamsNamedFooSuffix() {
347 const p = paramsPsNamedFooSuffixRoute.useParams()
348 return (
349 <div>
350 <h3>ParamsNamedFooSuffix</h3>
351 <div data-testid="params-output">{JSON.stringify(p)}</div>
352 </div>
353 )
354 },
355})
356β
357const paramsPsWildcardRoute = createRoute({
358 getParentRoute: () => paramsPsRoute,
359 path: '/wildcard',
360})
361β
362const paramsPsWildcardIndexRoute = createRoute({
363 getParentRoute: () => paramsPsWildcardRoute,
364 path: '/',
365 beforeLoad: () => {
366 throw redirect({ to: '/params-ps' })
367 },
368})
369β
370const paramsPsWildcardSplatRoute = createRoute({
371 getParentRoute: () => paramsPsWildcardRoute,
372 path: '$',
373 component: function ParamsWildcardSplat() {
374 const p = paramsPsWildcardSplatRoute.useParams()
375 return (
376 <div>
377 <h3>ParamsWildcardSplat</h3>
378 <div data-testid="params-output">{JSON.stringify(p)}</div>
379 </div>
380 )
381 },
382})
383β
384const paramsPsWildcardSplatPrefixRoute = createRoute({
385 getParentRoute: () => paramsPsWildcardRoute,
386 path: 'prefix{$}',
387 component: function ParamsWildcardSplatPrefix() {
388 const p = paramsPsWildcardSplatPrefixRoute.useParams()
389 return (
390 <div>
391 <h3>ParamsWildcardSplatPrefix</h3>
392 <div data-testid="params-output">{JSON.stringify(p)}</div>
393 </div>
394 )
395 },
396})
397β
398const paramsPsWildcardSplatSuffixRoute = createRoute({
399 getParentRoute: () => paramsPsWildcardRoute,
400 path: '{$}suffix',
401 component: function ParamsWildcardSplatSuffix() {
402 const p = paramsPsWildcardSplatSuffixRoute.useParams()
403 return (
404 <div>
405 <h3>ParamsWildcardSplatSuffix</h3>
406 <div data-testid="params-output">{JSON.stringify(p)}</div>
407 </div>
408 )
409 },
410})
411β
412const routeTree = rootRoute.addChildren([
413 postsRoute.addChildren([postRoute, postsIndexRoute]),
414 layoutRoute.addChildren([
415 layout2Route.addChildren([layoutARoute, layoutBRoute]),
416 ]),
417 paramsPsRoute.addChildren([
418 paramsPsNamedRoute.addChildren([
419 paramsPsNamedFooPrefixRoute,
420 paramsPsNamedFooSuffixRoute,
421 paramsPsNamedFooRoute,
422 paramsPsNamedIndexRoute,
423 ]),
424 paramsPsWildcardRoute.addChildren([
425 paramsPsWildcardSplatRoute,
426 paramsPsWildcardSplatPrefixRoute,
427 paramsPsWildcardSplatSuffixRoute,
428 paramsPsWildcardIndexRoute,
429 ]),
430 paramsPsIndexRoute,
431 ]),
432 indexRoute,
433])
434β
435// Set up a Router instance
436const router = createRouter({
437 routeTree,
438 defaultPreload: 'intent',
439 defaultStaleTime: 5000,
440 scrollRestoration: true,
441})
442β
443// Register things for typesafety
444declare module '@tanstack/react-router' {
445 interface Register {
446 router: typeof router
447 }
448}
449β
450const rootElement = document.getElementById('app')!
451β
452if (!rootElement.innerHTML) {
453 const root = ReactDOM.createRoot(rootElement)
454β
455 root.render(<RouterProvider router={router} />)
456}