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

RouterProvider.tsx

81 lines2.0 KB

1import * as React from 'react'
2import { Matches } from './Matches'
3import { getRouterContext } from './routerContext'
4import type {
5 AnyRouter,
6 RegisteredRouter,
7 RouterOptions,
8} from '@tanstack/router-core'
9​
10export function RouterContextProvider<
11 TRouter extends AnyRouter = RegisteredRouter,
12 TDehydrated extends Record<string, any> = Record<string, any>,
13>({
14 router,
15 children,
16 ...rest
17}: RouterProps<TRouter, TDehydrated> & {
18 children: React.ReactNode
19}) {
20 if (Object.keys(rest).length > 0) {
21 // Allow the router to update options on the router instance
22 router.update({
23 ...router.options,
24 ...rest,
25 context: {
26 ...router.options.context,
27 ...rest.context,
28 },
29 } as any)
30 }
31​
32 const routerContext = getRouterContext()
33​
34 const provider = (
35 <routerContext.Provider value={router as AnyRouter}>
36 {children}
37 </routerContext.Provider>
38 )
39​
40 if (router.options.Wrap) {
41 return <router.options.Wrap>{provider}</router.options.Wrap>
42 }
43​
44 return provider
46​
47export function RouterProvider<
48 TRouter extends AnyRouter = RegisteredRouter,
49 TDehydrated extends Record<string, any> = Record<string, any>,
50>({ router, ...rest }: RouterProps<TRouter, TDehydrated>) {
51 return (
52 <RouterContextProvider router={router} {...rest}>
53 <Matches />
54 </RouterContextProvider>
55 )
57​
58export type RouterProps<
59 TRouter extends AnyRouter = RegisteredRouter,
60 TDehydrated extends Record<string, any> = Record<string, any>,
61> = Omit<
62 RouterOptions<
63 TRouter['routeTree'],
64 NonNullable<TRouter['options']['trailingSlash']>,
65 NonNullable<TRouter['options']['defaultStructuralSharing']>,
66 TRouter['history'],
67 TDehydrated
68 >,
69 'context'
70> & {
71 router: TRouter
72 context?: Partial<
73 RouterOptions<
74 TRouter['routeTree'],
75 NonNullable<TRouter['options']['trailingSlash']>,
76 NonNullable<TRouter['options']['defaultStructuralSharing']>,
77 TRouter['history'],
78 TDehydrated
79 >['context']
80 >