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

main.tsx

55 lines1.6 KB

1import React from 'react'
2import ReactDOM from 'react-dom/client'
3import { RouterProvider, createRouter } from '@tanstack/react-router'
4import { routeTree } from './routeTree.gen'
5import './styles.css'
6​
7// Set up a Router instance
8const router = createRouter({
9 routeTree,
10 defaultPreload: 'intent',
11 defaultStaleTime: 5000,
12 scrollRestoration: true,
13 /*
14 Using defaultViewTransition would prevent the need to
15 manually add `viewTransition: true` to every navigation.
16​
17 If defaultViewTransition.types is a function, it will be called with the
18 location change info and should return an array of view transition types.
19 This is useful if you want to have different view transitions depending on
20 the navigation's specifics.
21​
22 An example use case is sliding in a direction based on the index of the
23 previous and next routes when navigating via browser history back and forth.
24 */
25 // defaultViewTransition: true
26 // OR
27 // defaultViewTransition: {
28 // types: ({ fromLocation, toLocation }) => {
29 // let direction = 'none'
30​
31 // if (fromLocation) {
32 // const fromIndex = fromLocation.state.__TSR_index
33 // const toIndex = toLocation.state.__TSR_index
34​
35 // direction = fromIndex > toIndex ? 'right' : 'left'
36 // }
37​
38 // return [`slide-${direction}`]
39 // },
40 // },
41})
42​
43// Register things for typesafety
44declare module '@tanstack/react-router' {
45 interface Register {
46 router: typeof router
47 }
49​
50const rootElement = document.getElementById('app')!
51​
52if (!rootElement.innerHTML) {
53 const root = ReactDOM.createRoot(rootElement)
54 root.render(<RouterProvider router={router} />)