Skip to content
Source
Sign inSign up
TanStack/router
Open History to see the last commit

main.tsx

440 lines10.2 KB

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>
74 <Outlet />
75 <TanStackRouterDevtools position="bottom-right" />
76 </>
77 )
79const indexRoute = createRoute({
80 getParentRoute: () => rootRoute,
81 path: '/',
82 component: IndexComponent,
83})
84​
85function IndexComponent() {
86 return (
87 <div className="p-2">
88 <h3>Welcome Home!</h3>
89 </div>
90 )
92​
93export const postsRoute = createRoute({
94 getParentRoute: () => rootRoute,
95 path: 'posts',
96 loader: () => fetchPosts(),
97}).lazy(() => import('./posts.lazy').then((d) => d.Route))
98​
99const postsIndexRoute = createRoute({
100 getParentRoute: () => postsRoute,
101 path: '/',
102 component: PostsIndexComponent,
103})
104​
105function PostsIndexComponent() {
106 return <div>Select a post.</div>
108​
109const postRoute = createRoute({
110 getParentRoute: () => postsRoute,
111 path: '$postId',
112 errorComponent: PostErrorComponent,
113 loader: ({ params }) => fetchPost(params.postId),
114 component: PostComponent,
115})
116​
117function PostErrorComponent({ error }: ErrorComponentProps) {
118 if (error instanceof NotFoundError) {
119 return <div>{error.message}</div>
120 }
121​
122 return <ErrorComponent error={error} />
124​
125function PostComponent() {
126 const post = postRoute.useLoaderData()
127​
128 return (
129 <div className="space-y-2">
130 <h4 className="text-xl font-bold">{post.title}</h4>
131 <hr className="opacity-20" />
132 <div className="text-sm">{post.body}</div>
133 </div>
134 )
136​
137const layoutRoute = createRoute({
138 getParentRoute: () => rootRoute,
139 id: '_layout',
140 component: LayoutComponent,
141})
142​
143function LayoutComponent() {
144 return (
145 <div className="p-2">
146 <div className="border-b">I'm a layout</div>
147 <div>
148 <Outlet />
149 </div>
150 </div>
151 )
153​
154const layout2Route = createRoute({
155 getParentRoute: () => layoutRoute,
156 id: '_layout-2',
157 component: Layout2Component,
158})
159​
160function Layout2Component() {
161 return (
162 <div>
163 <div>I'm a nested layout</div>
164 <div className="flex gap-2 border-b">
165 <Link
166 to="/layout-a"
167 activeProps={{
168 className: 'font-bold',
169 }}
170 >
171 Layout A
172 </Link>
173 <Link
174 to="/layout-b"
175 activeProps={{
176 className: 'font-bold',
177 }}
178 >
179 Layout B
180 </Link>
181 </div>
182 <div>
183 <Outlet />
184 </div>
185 </div>
186 )
188​
189const layoutARoute = createRoute({
190 getParentRoute: () => layout2Route,
191 path: '/layout-a',
192 component: LayoutAComponent,
193})
194​
195function LayoutAComponent() {
196 return <div>I'm layout A!</div>
198​
199const layoutBRoute = createRoute({
200 getParentRoute: () => layout2Route,
201 path: '/layout-b',
202 component: LayoutBComponent,
203})
204​
205function LayoutBComponent() {
206 return <div>I'm layout B!</div>
208​
209const paramsPsRoute = createRoute({
210 getParentRoute: () => rootRoute,
211 path: '/params-ps',
212})
213​
214const paramsPsIndexRoute = createRoute({
215 getParentRoute: () => paramsPsRoute,
216 path: '/',
217 component: function ParamsIndex() {
218 return (
219 <div>
220 <h3 className="pb-2">Named path params</h3>
221 <ul className="grid mb-2">
222 <li>
223 <Link
224 data-testid="l-to-named-foo"
225 to="/params-ps/named/$foo"
226 params={{ foo: 'foo' }}
227 >
228 /params-ps/named/$foo
229 </Link>
230 </li>
231 <li>
232 <Link
233 data-testid="l-to-named-prefixfoo"
234 to="/params-ps/named/prefix{$foo}"
235 params={{ foo: 'foo' }}
236 >
237 /params-ps/named/{'prefix{$foo}'}
238 </Link>
239 </li>
240 <li>
241 <Link
242 data-testid="l-to-named-foosuffix"
243 to="/params-ps/named/{$foo}suffix"
244 params={{ foo: 'foo' }}
245 >
246 /params-ps/named/{'{$foo}suffix'}
247 </Link>
248 </li>
249 </ul>
250 <hr />
251 <h3 className="pb-2">Wildcard path params</h3>
252 <ul className="grid mb-2">
253 <li>
254 <Link
255 data-testid="l-to-wildcard-foo"
256 to="/params-ps/wildcard/$"
257 params={{ _splat: 'foo' }}
258 >
259 /params-ps/wildcard/$
260 </Link>
261 </li>
262 <li>
263 <Link
264 data-testid="l-to-wildcard-prefixfoo"
265 to="/params-ps/wildcard/prefix{$}"
266 params={{ _splat: 'foo' }}
267 >
268 /params-ps/wildcard/{'prefix{$}'}
269 </Link>
270 </li>
271 <li>
272 <Link
273 data-testid="l-to-wildcard-foosuffix"
274 to="/params-ps/wildcard/{$}suffix"
275 params={{ _splat: 'foo' }}
276 >
277 /params-ps/wildcard/{'{$}suffix'}
278 </Link>
279 </li>
280 </ul>
281 </div>
282 )
283 },
284})
285​
286const paramsPsNamedRoute = createRoute({
287 getParentRoute: () => paramsPsRoute,
288 path: '/named',
289})
290​
291const paramsPsNamedIndexRoute = createRoute({
292 getParentRoute: () => paramsPsNamedRoute,
293 path: '/',
294 beforeLoad: () => {
295 throw redirect({ to: '/params-ps' })
296 },
297})
298​
299const paramsPsNamedFooRoute = createRoute({
300 getParentRoute: () => paramsPsNamedRoute,
301 path: '/$foo',
302 component: function ParamsNamedFoo() {
303 const p = paramsPsNamedFooRoute.useParams()
304 return (
305 <div>
306 <h3>ParamsNamedFoo</h3>
307 <div data-testid="params-output">{JSON.stringify(p)}</div>
308 </div>
309 )
310 },
311})
312​
313const paramsPsNamedFooPrefixRoute = createRoute({
314 getParentRoute: () => paramsPsNamedRoute,
315 path: '/prefix{$foo}',
316 component: function ParamsNamedFooMarkdown() {
317 const p = paramsPsNamedFooPrefixRoute.useParams()
318 return (
319 <div>
320 <h3>ParamsNamedFooPrefix</h3>
321 <div data-testid="params-output">{JSON.stringify(p)}</div>
322 </div>
323 )
324 },
325})
326​
327const paramsPsNamedFooSuffixRoute = createRoute({
328 getParentRoute: () => paramsPsNamedRoute,
329 path: '/{$foo}suffix',
330 component: function ParamsNamedFooSuffix() {
331 const p = paramsPsNamedFooSuffixRoute.useParams()
332 return (
333 <div>
334 <h3>ParamsNamedFooSuffix</h3>
335 <div data-testid="params-output">{JSON.stringify(p)}</div>
336 </div>
337 )
338 },
339})
340​
341const paramsPsWildcardRoute = createRoute({
342 getParentRoute: () => paramsPsRoute,
343 path: '/wildcard',
344})
345​
346const paramsPsWildcardIndexRoute = createRoute({
347 getParentRoute: () => paramsPsWildcardRoute,
348 path: '/',
349 beforeLoad: () => {
350 throw redirect({ to: '/params-ps' })
351 },
352})
353​
354const paramsPsWildcardSplatRoute = createRoute({
355 getParentRoute: () => paramsPsWildcardRoute,
356 path: '$',
357 component: function ParamsWildcardSplat() {
358 const p = paramsPsWildcardSplatRoute.useParams()
359 return (
360 <div>
361 <h3>ParamsWildcardSplat</h3>
362 <div data-testid="params-output">{JSON.stringify(p)}</div>
363 </div>
364 )
365 },
366})
367​
368const paramsPsWildcardSplatPrefixRoute = createRoute({
369 getParentRoute: () => paramsPsWildcardRoute,
370 path: 'prefix{$}',
371 component: function ParamsWildcardSplatPrefix() {
372 const p = paramsPsWildcardSplatPrefixRoute.useParams()
373 return (
374 <div>
375 <h3>ParamsWildcardSplatPrefix</h3>
376 <div data-testid="params-output">{JSON.stringify(p)}</div>
377 </div>
378 )
379 },
380})
381​
382const paramsPsWildcardSplatSuffixRoute = createRoute({
383 getParentRoute: () => paramsPsWildcardRoute,
384 path: '{$}suffix',
385 component: function ParamsWildcardSplatSuffix() {
386 const p = paramsPsWildcardSplatSuffixRoute.useParams()
387 return (
388 <div>
389 <h3>ParamsWildcardSplatSuffix</h3>
390 <div data-testid="params-output">{JSON.stringify(p)}</div>
391 </div>
392 )
393 },
394})
395​
396const routeTree = rootRoute.addChildren([
397 postsRoute.addChildren([postRoute, postsIndexRoute]),
398 layoutRoute.addChildren([
399 layout2Route.addChildren([layoutARoute, layoutBRoute]),
400 ]),
401 paramsPsRoute.addChildren([
402 paramsPsNamedRoute.addChildren([
403 paramsPsNamedFooPrefixRoute,
404 paramsPsNamedFooSuffixRoute,
405 paramsPsNamedFooRoute,
406 paramsPsNamedIndexRoute,
407 ]),
408 paramsPsWildcardRoute.addChildren([
409 paramsPsWildcardSplatRoute,
410 paramsPsWildcardSplatPrefixRoute,
411 paramsPsWildcardSplatSuffixRoute,
412 paramsPsWildcardIndexRoute,
413 ]),
414 paramsPsIndexRoute,
415 ]),
416 indexRoute,
417])
418​
419// Set up a Router instance
420const router = createRouter({
421 routeTree,
422 defaultPreload: 'intent',
423 defaultStaleTime: 5000,
424 scrollRestoration: true,
425})
426​
427// Register things for typesafety
428declare module '@tanstack/react-router' {
429 interface Register {
430 router: typeof router
431 }
433​
434const rootElement = document.getElementById('app')!
435​
436if (!rootElement.innerHTML) {
437 const root = ReactDOM.createRoot(rootElement)
438​
439 root.render(<RouterProvider router={router} />)