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

main.tsx

232 lines5.0 KB

1import { render } from 'solid-js/web'
2import {
3 ErrorComponent,
4 Link,
5 Outlet,
6 RouterProvider,
7 createRootRoute,
8 createRoute,
9 createRouter,
10} from '@tanstack/solid-router'
11import { TanStackRouterDevtools } from '@tanstack/solid-router-devtools'
12import { NotFoundError, fetchPost, fetchPosts } from './posts'
13import type { ErrorComponentProps } from '@tanstack/solid-router'
14import './styles.css'
15​
16const rootRoute = createRootRoute({
17 component: RootComponent,
18 notFoundComponent: () => {
19 return (
20 <div>
21 <p>This is the notFoundComponent configured on root route</p>
22 <Link to="/">Start Over</Link>
23 </div>
24 )
25 },
26})
27​
28function RootComponent() {
29 return (
30 <>
31 <div class="p-2 flex gap-2 text-lg border-b">
32 <Link
33 to="/"
34 activeProps={{
35 class: 'font-bold',
36 }}
37 activeOptions={{ exact: true }}
38 >
39 Home
40 </Link>{' '}
41 <Link
42 to="/posts"
43 activeProps={{
44 class: 'font-bold',
45 }}
46 >
47 Posts
48 </Link>{' '}
49 <Link
50 to="/route-a"
51 activeProps={{
52 class: 'font-bold',
53 }}
54 >
55 Pathless Layout
56 </Link>{' '}
57 <Link
58 // @ts-expect-error
59 to="/this-route-does-not-exist"
60 activeProps={{
61 class: 'font-bold',
62 }}
63 >
64 This Route Does Not Exist
65 </Link>
66 </div>
67 <Outlet />
68 <TanStackRouterDevtools position="bottom-right" />
69 </>
70 )
72const indexRoute = createRoute({
73 getParentRoute: () => rootRoute,
74 path: '/',
75 component: IndexComponent,
76})
77​
78function IndexComponent() {
79 return (
80 <div class="p-2">
81 <h3>Welcome Home!</h3>
82 </div>
83 )
85​
86export const postsLayoutRoute = createRoute({
87 getParentRoute: () => rootRoute,
88 path: 'posts',
89 loader: () => fetchPosts(),
90}).lazy(() => import('./posts.lazy').then((d) => d.Route))
91​
92const postsIndexRoute = createRoute({
93 getParentRoute: () => postsLayoutRoute,
94 path: '/',
95 component: PostsIndexComponent,
96})
97​
98function PostsIndexComponent() {
99 return <div>Select a post.</div>
101​
102const postRoute = createRoute({
103 getParentRoute: () => postsLayoutRoute,
104 path: '$postId',
105 errorComponent: PostErrorComponent,
106 loader: ({ params }) => fetchPost(params.postId),
107 component: PostComponent,
108})
109​
110function PostErrorComponent({ error }: ErrorComponentProps) {
111 if (error instanceof NotFoundError) {
112 return <div>{error.message}</div>
113 }
114​
115 return <ErrorComponent error={error} />
117​
118function PostComponent() {
119 const post = postRoute.useLoaderData()
120​
121 return (
122 <div class="space-y-2">
123 <h4 class="text-xl font-bold">{post().title}</h4>
124 <hr class="opacity-20" />
125 <div class="text-sm">{post().body}</div>
126 </div>
127 )
129​
130const pathlessLayoutRoute = createRoute({
131 getParentRoute: () => rootRoute,
132 id: '_pathlessLayout',
133 component: PathlessLayoutComponent,
134})
135​
136function PathlessLayoutComponent() {
137 return (
138 <div class="p-2">
139 <div class="border-b">I'm a pathless layout</div>
140 <div>
141 <Outlet />
142 </div>
143 </div>
144 )
146​
147const nestedPathlessLayout2Route = createRoute({
148 getParentRoute: () => pathlessLayoutRoute,
149 id: '_nestedPathlessLayout',
150 component: PathlessLayout2Component,
151})
152​
153function PathlessLayout2Component() {
154 return (
155 <div>
156 <div>I'm a nested pathless layout</div>
157 <div class="flex gap-2 border-b">
158 <Link
159 to="/route-a"
160 activeProps={{
161 class: 'font-bold',
162 }}
163 >
164 Go to Route A
165 </Link>
166 <Link
167 to="/route-b"
168 activeProps={{
169 class: 'font-bold',
170 }}
171 >
172 Go to Route B
173 </Link>
174 </div>
175 <div>
176 <Outlet />
177 </div>
178 </div>
179 )
181​
182const pathlessLayoutARoute = createRoute({
183 getParentRoute: () => nestedPathlessLayout2Route,
184 path: '/route-a',
185 component: PathlessLayoutAComponent,
186})
187​
188function PathlessLayoutAComponent() {
189 return <div>I'm route A!</div>
191​
192const pathlessLayoutBRoute = createRoute({
193 getParentRoute: () => nestedPathlessLayout2Route,
194 path: '/route-b',
195 component: PathlessLayoutBComponent,
196})
197​
198function PathlessLayoutBComponent() {
199 return <div>I'm route B!</div>
201​
202const routeTree = rootRoute.addChildren([
203 postsLayoutRoute.addChildren([postRoute, postsIndexRoute]),
204 pathlessLayoutRoute.addChildren([
205 nestedPathlessLayout2Route.addChildren([
206 pathlessLayoutARoute,
207 pathlessLayoutBRoute,
208 ]),
209 ]),
210 indexRoute,
211])
212​
213// Set up a Router instance
214const router = createRouter({
215 routeTree,
216 defaultPreload: 'intent',
217 defaultStaleTime: 5000,
218 scrollRestoration: true,
219})
220​
221// Register things for typesafety
222declare module '@tanstack/solid-router' {
223 interface Register {
224 router: typeof router
225 }
227​
228const rootElement = document.getElementById('app')!
229​
230if (!rootElement.innerHTML) {
231 render(() => <RouterProvider router={router} />, rootElement)