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

router.ts

119 lines4.8 KB

1import { RouterCore } from '@tanstack/router-core'
2import { getStoreFactory } from './routerStores'
3import type { RouterHistory } from '@tanstack/history'
4import type {
5 AnyRoute,
6 CreateRouterFn,
7 RouterConstructorOptions,
8 TrailingSlashOption,
9} from '@tanstack/router-core'
10​
11import type {
12 ErrorRouteComponent,
13 NotFoundRouteComponent,
14 RouteComponent,
15} from './route'
16​
17declare module '@tanstack/router-core' {
18 export interface RouterOptionsExtensions {
19 /**
20 * The default `component` a route should use if no component is provided.
21 *
22 * @default Outlet
23 * @link [API Docs](https://tanstack.com/router/latest/docs/framework/react/api/router/RouterOptionsType#defaultcomponent-property)
24 */
25 defaultComponent?: RouteComponent
26 /**
27 * The default `errorComponent` a route should use if no error component is provided.
28 *
29 * @default ErrorComponent
30 * @link [API Docs](https://tanstack.com/router/latest/docs/framework/react/api/router/RouterOptionsType#defaulterrorcomponent-property)
31 * @link [Guide](https://tanstack.com/router/latest/docs/framework/react/guide/data-loading#handling-errors-with-routeoptionserrorcomponent)
32 */
33 defaultErrorComponent?: ErrorRouteComponent
34 /**
35 * The default `pendingComponent` a route should use if no pending component is provided.
36 *
37 * @link [API Docs](https://tanstack.com/router/latest/docs/framework/react/api/router/RouterOptionsType#defaultpendingcomponent-property)
38 * @link [Guide](https://tanstack.com/router/latest/docs/framework/react/guide/data-loading#showing-a-pending-component)
39 */
40 defaultPendingComponent?: RouteComponent
41 /**
42 * The default `notFoundComponent` a route should use if no notFound component is provided.
43 *
44 * @default NotFound
45 * @link [API Docs](https://tanstack.com/router/latest/docs/framework/react/api/router/RouterOptionsType#defaultnotfoundcomponent-property)
46 * @link [Guide](https://tanstack.com/router/latest/docs/framework/react/guide/not-found-errors#default-router-wide-not-found-handling)
47 */
48 defaultNotFoundComponent?: NotFoundRouteComponent
49 /**
50 * A component that will be used to wrap the entire router.
51 *
52 * This is useful for providing a context to the entire router.
53 *
54 * Only non-DOM-rendering components like providers should be used, anything else will cause a hydration error.
55 *
56 * @link [API Docs](https://tanstack.com/router/latest/docs/framework/react/api/router/RouterOptionsType#wrap-property)
57 */
58 Wrap?: (props: { children: any }) => React.JSX.Element
59 /**
60 * A component that will be used to wrap the inner contents of the router.
61 *
62 * This is useful for providing a context to the inner contents of the router where you also need access to the router context and hooks.
63 *
64 * Only non-DOM-rendering components like providers should be used, anything else will cause a hydration error.
65 *
66 * @link [API Docs](https://tanstack.com/router/latest/docs/framework/react/api/router/RouterOptionsType#innerwrap-property)
67 */
68 InnerWrap?: (props: { children: any }) => React.JSX.Element
69​
70 /**
71 * The default `onCatch` handler for errors caught by the Router ErrorBoundary
72 *
73 * @link [API Docs](https://tanstack.com/router/latest/docs/framework/react/api/router/RouterOptionsType#defaultoncatch-property)
74 * @link [Guide](https://tanstack.com/router/latest/docs/framework/react/guide/data-loading#handling-errors-with-routeoptionsoncatch)
75 */
76 defaultOnCatch?: (error: unknown, errorInfo: React.ErrorInfo) => void
77 }
79​
80/**
81 * Creates a new Router instance for React.
82 *
83 * Pass the returned router to `RouterProvider` to enable routing.
84 * Notable options: `routeTree` (your route definitions) and `context`
85 * (required if the root route was created with `createRootRouteWithContext`).
86 *
87 * @param options Router options used to configure the router.
88 * @returns A Router instance to be provided to `RouterProvider`.
89 * @link https://tanstack.com/router/latest/docs/framework/react/api/router/createRouterFunction
90 */
91export const createRouter: CreateRouterFn = (options) => {
92 return new Router(options)
94​
95export class Router<
96 in out TRouteTree extends AnyRoute,
97 in out TTrailingSlashOption extends TrailingSlashOption = 'never',
98 in out TDefaultStructuralSharingOption extends boolean = false,
99 in out TRouterHistory extends RouterHistory = RouterHistory,
100 in out TDehydrated extends Record<string, any> = Record<string, any>,
101> extends RouterCore<
102 TRouteTree,
103 TTrailingSlashOption,
104 TDefaultStructuralSharingOption,
105 TRouterHistory,
106 TDehydrated
107> {
108 constructor(
109 options: RouterConstructorOptions<
110 TRouteTree,
111 TTrailingSlashOption,
112 TDefaultStructuralSharingOption,
113 TRouterHistory,
114 TDehydrated
115 >,
116 ) {
117 super(options, getStoreFactory)
118 }