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

routerContext.tsx

24 lines521 Bytes

1import * as React from 'react'
2import type { AnyRouter } from '@tanstack/router-core'
3​
4declare global {
5 interface Window {
6 __TSR_ROUTER_CONTEXT__?: React.Context<AnyRouter>
7 }
8}
9​
10const routerContext = React.createContext<AnyRouter>(null!)
11​
12export function getRouterContext() {
13 if (typeof document === 'undefined') {
14 return routerContext
15 }
16​
17 if (window.__TSR_ROUTER_CONTEXT__) {
18 return window.__TSR_ROUTER_CONTEXT__
19 }
20​
21 window.__TSR_ROUTER_CONTEXT__ = routerContext as any
22​
23 return routerContext