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

main.tsx

59 lines1.3 KB

1import React from 'react'
2import ReactDOM from 'react-dom/client'
3import { RouterProvider, createRouter } from '@tanstack/react-router'
4​
5import { routeTree } from './routeTree.gen'
6import { AuthContextProvider, useAuth } from './auth'
7​
8import './styles.css'
9​
10// Set up a Router instance
11const router = createRouter({
12 routeTree,
13 defaultPreload: 'intent',
14 scrollRestoration: true,
15 context: {
16 auth: undefined!, // This will be set after we wrap the app in AuthContextProvider
17 },
18})
19​
20// Register things for typesafety
21declare module '@tanstack/react-router' {
22 interface Register {
23 router: typeof router
24 }
26​
27function InnerApp() {
28 const auth = useAuth()
29​
30 // If the provider is initially loading, do not render the router
31 if (auth.isInitialLoading) {
32 return (
33 <div className="flex h-screen w-full items-center justify-center p-4">
34 <div className="size-10 rounded-full border-4 border-gray-200 border-t-foreground animate-spin" />
35 </div>
36 )
37 }
38​
39 return <RouterProvider router={router} context={{ auth }} />
41​
42function App() {
43 return (
44 <AuthContextProvider>
45 <InnerApp />
46 </AuthContextProvider>
47 )
49​
50const rootElement = document.getElementById('app')!
51​
52if (!rootElement.innerHTML) {
53 const root = ReactDOM.createRoot(rootElement)
54 root.render(
55 <React.StrictMode>
56 <App />
57 </React.StrictMode>,
58 )