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

RouterProvider.tsx

78 lines1.9 KB

1import { getRouterContext } from './routerContext'
2import { SafeFragment } from './SafeFragment'
3import { Matches } from './Matches'
4import type {
5 AnyRouter,
6 RegisteredRouter,
7 RouterOptions,
8} from '@tanstack/router-core'
9import type * as Solid from 'solid-js'
10​
11export function RouterContextProvider<
12 TRouter extends AnyRouter = RegisteredRouter,
13 TDehydrated extends Record<string, any> = Record<string, any>,
14>({
15 router,
16 children,
17 ...rest
18}: RouterProps<TRouter, TDehydrated> & {
19 children: () => Solid.JSX.Element
20}) {
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 const routerContext = getRouterContext()
32​
33 const OptionalWrapper = router.options.Wrap || SafeFragment
34​
35 return (
36 <OptionalWrapper>
37 <routerContext.Provider value={router as AnyRouter}>
38 {children()}
39 </routerContext.Provider>
40 </OptionalWrapper>
41 )
43​
44export function RouterProvider<
45 TRouter extends AnyRouter = RegisteredRouter,
46 TDehydrated extends Record<string, any> = Record<string, any>,
47>({ router, ...rest }: RouterProps<TRouter, TDehydrated>) {
48 return (
49 <RouterContextProvider router={router} {...rest}>
50 {() => <Matches />}
51 </RouterContextProvider>
52 )
54​
55export type RouterProps<
56 TRouter extends AnyRouter = RegisteredRouter,
57 TDehydrated extends Record<string, any> = Record<string, any>,
58> = Omit<
59 RouterOptions<
60 TRouter['routeTree'],
61 NonNullable<TRouter['options']['trailingSlash']>,
62 false,
63 TRouter['history'],
64 TDehydrated
65 >,
66 'context'
67> & {
68 router: TRouter
69 context?: Partial<
70 RouterOptions<
71 TRouter['routeTree'],
72 NonNullable<TRouter['options']['trailingSlash']>,
73 false,
74 TRouter['history'],
75 TDehydrated
76 >['context']
77 >