@tanstack/react-router 0.0.1-beta.28 → 0.0.1-beta.281

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (106) hide show
  1. package/LICENSE +21 -0
  2. package/build/cjs/CatchBoundary.js +128 -0
  3. package/build/cjs/CatchBoundary.js.map +1 -0
  4. package/build/cjs/Matches.js +233 -0
  5. package/build/cjs/Matches.js.map +1 -0
  6. package/build/cjs/RouterProvider.js +170 -0
  7. package/build/cjs/RouterProvider.js.map +1 -0
  8. package/build/cjs/_virtual/_rollupPluginBabelHelpers.js +2 -22
  9. package/build/cjs/_virtual/_rollupPluginBabelHelpers.js.map +1 -1
  10. package/build/cjs/awaited.js +43 -0
  11. package/build/cjs/awaited.js.map +1 -0
  12. package/build/cjs/defer.js +37 -0
  13. package/build/cjs/defer.js.map +1 -0
  14. package/build/cjs/fileRoute.js +27 -0
  15. package/build/cjs/fileRoute.js.map +1 -0
  16. package/build/cjs/index.js +130 -0
  17. package/build/cjs/index.js.map +1 -0
  18. package/build/cjs/lazyRouteComponent.js +54 -0
  19. package/build/cjs/lazyRouteComponent.js.map +1 -0
  20. package/build/cjs/link.js +223 -0
  21. package/build/cjs/link.js.map +1 -0
  22. package/build/cjs/path.js +214 -0
  23. package/build/cjs/path.js.map +1 -0
  24. package/build/cjs/qss.js +63 -0
  25. package/build/cjs/qss.js.map +1 -0
  26. package/build/cjs/redirects.js +28 -0
  27. package/build/cjs/redirects.js.map +1 -0
  28. package/build/cjs/route.js +191 -0
  29. package/build/cjs/route.js.map +1 -0
  30. package/build/cjs/router.js +1067 -0
  31. package/build/cjs/router.js.map +1 -0
  32. package/build/cjs/scroll-restoration.js +202 -0
  33. package/build/cjs/scroll-restoration.js.map +1 -0
  34. package/build/cjs/searchParams.js +81 -0
  35. package/build/cjs/searchParams.js.map +1 -0
  36. package/build/cjs/useBlocker.js +55 -0
  37. package/build/cjs/useBlocker.js.map +1 -0
  38. package/build/cjs/useNavigate.js +86 -0
  39. package/build/cjs/useNavigate.js.map +1 -0
  40. package/build/cjs/useParams.js +26 -0
  41. package/build/cjs/useParams.js.map +1 -0
  42. package/build/cjs/useSearch.js +25 -0
  43. package/build/cjs/useSearch.js.map +1 -0
  44. package/build/cjs/utils.js +239 -0
  45. package/build/cjs/utils.js.map +1 -0
  46. package/build/esm/index.js +2282 -2580
  47. package/build/esm/index.js.map +1 -1
  48. package/build/stats-html.html +3498 -2694
  49. package/build/stats-react.json +1204 -44
  50. package/build/types/CatchBoundary.d.ts +36 -0
  51. package/build/types/Matches.d.ts +63 -0
  52. package/build/types/RouterProvider.d.ts +35 -0
  53. package/build/types/awaited.d.ts +9 -0
  54. package/build/types/defer.d.ts +19 -0
  55. package/build/types/fileRoute.d.ts +38 -0
  56. package/build/types/history.d.ts +7 -0
  57. package/build/types/index.d.ts +27 -114
  58. package/build/types/lazyRouteComponent.d.ts +2 -0
  59. package/build/types/link.d.ts +91 -0
  60. package/build/types/location.d.ts +12 -0
  61. package/build/types/path.d.ts +17 -0
  62. package/build/types/qss.d.ts +2 -0
  63. package/build/types/redirects.d.ts +11 -0
  64. package/build/types/route.d.ts +283 -0
  65. package/build/types/routeInfo.d.ts +22 -0
  66. package/build/types/router.d.ts +185 -0
  67. package/build/types/scroll-restoration.d.ts +18 -0
  68. package/build/types/searchParams.d.ts +7 -0
  69. package/build/types/useBlocker.d.ts +9 -0
  70. package/build/types/useNavigate.d.ts +19 -0
  71. package/build/types/useParams.d.ts +7 -0
  72. package/build/types/useSearch.d.ts +7 -0
  73. package/build/types/utils.d.ts +66 -0
  74. package/build/umd/index.development.js +2873 -2545
  75. package/build/umd/index.development.js.map +1 -1
  76. package/build/umd/index.production.js +4 -4
  77. package/build/umd/index.production.js.map +1 -1
  78. package/package.json +11 -10
  79. package/src/CatchBoundary.tsx +101 -0
  80. package/src/Matches.tsx +422 -0
  81. package/src/RouterProvider.tsx +252 -0
  82. package/src/awaited.tsx +40 -0
  83. package/src/defer.ts +55 -0
  84. package/src/fileRoute.ts +152 -0
  85. package/src/history.ts +8 -0
  86. package/src/index.tsx +28 -761
  87. package/src/lazyRouteComponent.tsx +33 -0
  88. package/src/link.tsx +598 -0
  89. package/src/location.ts +13 -0
  90. package/src/path.ts +261 -0
  91. package/src/qss.ts +53 -0
  92. package/src/redirects.ts +39 -0
  93. package/src/route.ts +882 -0
  94. package/src/routeInfo.ts +70 -0
  95. package/src/router.ts +1651 -0
  96. package/src/scroll-restoration.tsx +230 -0
  97. package/src/searchParams.ts +79 -0
  98. package/src/useBlocker.tsx +27 -0
  99. package/src/useNavigate.tsx +111 -0
  100. package/src/useParams.tsx +25 -0
  101. package/src/useSearch.tsx +25 -0
  102. package/src/utils.ts +350 -0
  103. package/build/cjs/react-router/src/index.js +0 -508
  104. package/build/cjs/react-router/src/index.js.map +0 -1
  105. package/build/cjs/router-core/build/esm/index.js +0 -2530
  106. package/build/cjs/router-core/build/esm/index.js.map +0 -1
package/package.json CHANGED
@@ -1,10 +1,10 @@
1
1
  {
2
2
  "name": "@tanstack/react-router",
3
3
  "author": "Tanner Linsley",
4
- "version": "0.0.1-beta.28",
4
+ "version": "0.0.1-beta.281",
5
5
  "license": "MIT",
6
6
  "repository": "tanstack/router",
7
- "homepage": "https://tanstack.com/router/",
7
+ "homepage": "https://tanstack.com/router",
8
8
  "description": "",
9
9
  "publishConfig": {
10
10
  "registry": "https://registry.npmjs.org/"
@@ -12,7 +12,6 @@
12
12
  "keywords": [
13
13
  "react",
14
14
  "location",
15
- "@tanstack/react-router",
16
15
  "router",
17
16
  "routing",
18
17
  "async",
@@ -24,7 +23,7 @@
24
23
  "url": "https://github.com/sponsors/tannerlinsley"
25
24
  },
26
25
  "module": "build/esm/index.js",
27
- "main": "build/cjs/react-router/src/index.js",
26
+ "main": "build/cjs/index.js",
28
27
  "browser": "build/umd/index.production.js",
29
28
  "types": "build/types/index.d.ts",
30
29
  "engines": {
@@ -41,11 +40,13 @@
41
40
  },
42
41
  "dependencies": {
43
42
  "@babel/runtime": "^7.16.7",
44
- "@tanstack/router-core": "0.0.1-beta.26",
45
- "use-sync-external-store": "^1.2.0"
43
+ "@tanstack/react-store": "^0.2.1",
44
+ "@tanstack/store": "^0.1.3",
45
+ "tiny-invariant": "^1.3.1",
46
+ "tiny-warning": "^1.0.3",
47
+ "@tanstack/history": "0.0.1-beta.281"
46
48
  },
47
- "devDependencies": {
48
- "@types/use-sync-external-store": "^0.0.3",
49
- "babel-plugin-transform-async-to-promises": "^0.8.18"
49
+ "scripts": {
50
+ "build": "rollup --config rollup.config.js"
50
51
  }
51
- }
52
+ }
@@ -0,0 +1,101 @@
1
+ import * as React from 'react'
2
+
3
+ export function CatchBoundary(props: {
4
+ getResetKey: () => string
5
+ children: any
6
+ errorComponent?: any
7
+ onCatch: (error: any) => void
8
+ }) {
9
+ const errorComponent = props.errorComponent ?? ErrorComponent
10
+
11
+ return (
12
+ <CatchBoundaryImpl
13
+ getResetKey={props.getResetKey}
14
+ onCatch={props.onCatch}
15
+ children={({ error }) => {
16
+ if (error) {
17
+ return React.createElement(errorComponent, {
18
+ error,
19
+ })
20
+ }
21
+
22
+ return props.children
23
+ }}
24
+ />
25
+ )
26
+ }
27
+
28
+ export class CatchBoundaryImpl extends React.Component<{
29
+ getResetKey: () => string
30
+ children: (props: { error: any; reset: () => void }) => any
31
+ onCatch?: (error: any) => void
32
+ }> {
33
+ state = { error: null } as any
34
+ static getDerivedStateFromProps(props: any) {
35
+ return { resetKey: props.getResetKey() }
36
+ }
37
+ static getDerivedStateFromError(error: any) {
38
+ return { error }
39
+ }
40
+ componentDidUpdate(
41
+ prevProps: Readonly<{
42
+ getResetKey: () => string
43
+ children: (props: { error: any; reset: () => void }) => any
44
+ onCatch?: ((error: any, info: any) => void) | undefined
45
+ }>,
46
+ prevState: any,
47
+ ): void {
48
+ if (prevState.error && prevState.resetKey !== this.state.resetKey) {
49
+ this.setState({ error: null })
50
+ }
51
+ }
52
+ componentDidCatch(error: any) {
53
+ console.error(error)
54
+ this.props.onCatch?.(error)
55
+ }
56
+ render() {
57
+ return this.props.children(this.state)
58
+ }
59
+ }
60
+
61
+ export function ErrorComponent({ error }: { error: any }) {
62
+ const [show, setShow] = React.useState(process.env.NODE_ENV !== 'production')
63
+
64
+ return (
65
+ <div style={{ padding: '.5rem', maxWidth: '100%' }}>
66
+ <div style={{ display: 'flex', alignItems: 'center', gap: '.5rem' }}>
67
+ <strong style={{ fontSize: '1rem' }}>Something went wrong!</strong>
68
+ <button
69
+ style={{
70
+ appearance: 'none',
71
+ fontSize: '.6em',
72
+ border: '1px solid currentColor',
73
+ padding: '.1rem .2rem',
74
+ fontWeight: 'bold',
75
+ borderRadius: '.25rem',
76
+ }}
77
+ onClick={() => setShow((d) => !d)}
78
+ >
79
+ {show ? 'Hide Error' : 'Show Error'}
80
+ </button>
81
+ </div>
82
+ <div style={{ height: '.25rem' }} />
83
+ {show ? (
84
+ <div>
85
+ <pre
86
+ style={{
87
+ fontSize: '.7em',
88
+ border: '1px solid red',
89
+ borderRadius: '.25rem',
90
+ padding: '.3rem',
91
+ color: 'red',
92
+ overflow: 'auto',
93
+ }}
94
+ >
95
+ {error.message ? <code>{error.message}</code> : null}
96
+ </pre>
97
+ </div>
98
+ ) : null}
99
+ </div>
100
+ )
101
+ }
@@ -0,0 +1,422 @@
1
+ import * as React from 'react'
2
+ import invariant from 'tiny-invariant'
3
+ import warning from 'tiny-warning'
4
+ import { CatchBoundary, ErrorComponent } from './CatchBoundary'
5
+ import { useRouter, useRouterState } from './RouterProvider'
6
+ import { ResolveRelativePath, ToOptions } from './link'
7
+ import { AnyRoute, ReactNode, rootRouteId } from './route'
8
+ import {
9
+ FullSearchSchema,
10
+ ParseRoute,
11
+ RouteById,
12
+ RouteByPath,
13
+ RouteIds,
14
+ RoutePaths,
15
+ } from './routeInfo'
16
+ import { RegisteredRouter, RouterState } from './router'
17
+ import { NoInfer, StrictOrFrom, pick } from './utils'
18
+
19
+ export const matchContext = React.createContext<string | undefined>(undefined)
20
+
21
+ export interface RouteMatch<
22
+ TRouteTree extends AnyRoute = AnyRoute,
23
+ TRouteId extends RouteIds<TRouteTree> = ParseRoute<TRouteTree>['id'],
24
+ > {
25
+ id: string
26
+ routeId: TRouteId
27
+ pathname: string
28
+ params: RouteById<TRouteTree, TRouteId>['types']['allParams']
29
+ status: 'pending' | 'success' | 'error'
30
+ isFetching: boolean
31
+ showPending: boolean
32
+ error: unknown
33
+ paramsError: unknown
34
+ searchError: unknown
35
+ updatedAt: number
36
+ loadPromise?: Promise<void>
37
+ loaderData?: RouteById<TRouteTree, TRouteId>['types']['loaderData']
38
+ routeContext: RouteById<TRouteTree, TRouteId>['types']['routeContext']
39
+ context: RouteById<TRouteTree, TRouteId>['types']['allContext']
40
+ search: FullSearchSchema<TRouteTree> &
41
+ RouteById<TRouteTree, TRouteId>['types']['fullSearchSchema']
42
+ fetchCount: number
43
+ abortController: AbortController
44
+ cause: 'preload' | 'enter' | 'stay'
45
+ loaderDeps: RouteById<TRouteTree, TRouteId>['types']['loaderDeps']
46
+ invalid: boolean
47
+ }
48
+
49
+ export type AnyRouteMatch = RouteMatch<any, any>
50
+
51
+ export function Matches() {
52
+ const router = useRouter()
53
+ const matchId = useRouterState({
54
+ select: (s) => {
55
+ return getRenderedMatches(s)[0]?.id
56
+ },
57
+ })
58
+
59
+ return (
60
+ <matchContext.Provider value={matchId}>
61
+ <CatchBoundary
62
+ getResetKey={() => router.state.resolvedLocation.state?.key}
63
+ errorComponent={ErrorComponent}
64
+ onCatch={() => {
65
+ warning(
66
+ false,
67
+ `Error in router! Consider setting an 'errorComponent' in your RootRoute! 👍`,
68
+ )
69
+ }}
70
+ >
71
+ {matchId ? <Match matchId={matchId} /> : null}
72
+ </CatchBoundary>
73
+ </matchContext.Provider>
74
+ )
75
+ }
76
+
77
+ function SafeFragment(props: any) {
78
+ return <>{props.children}</>
79
+ }
80
+
81
+ export function Match({ matchId }: { matchId: string }) {
82
+ const router = useRouter()
83
+ const routeId = useRouterState({
84
+ select: (s) =>
85
+ getRenderedMatches(s).find((d) => d.id === matchId)?.routeId as string,
86
+ })
87
+
88
+ invariant(
89
+ routeId,
90
+ `Could not find routeId for matchId "${matchId}". Please file an issue!`,
91
+ )
92
+
93
+ const route = router.routesById[routeId]!
94
+
95
+ const PendingComponent = (route.options.pendingComponent ??
96
+ router.options.defaultPendingComponent) as any
97
+
98
+ const pendingElement = PendingComponent ? <PendingComponent /> : null
99
+
100
+ const routeErrorComponent =
101
+ route.options.errorComponent ??
102
+ router.options.defaultErrorComponent ??
103
+ ErrorComponent
104
+
105
+ const ResolvedSuspenseBoundary =
106
+ route.options.wrapInSuspense ??
107
+ PendingComponent ??
108
+ route.options.component?.preload ??
109
+ route.options.pendingComponent?.preload ??
110
+ (route.options.errorComponent as any)?.preload
111
+ ? React.Suspense
112
+ : SafeFragment
113
+
114
+ const ResolvedCatchBoundary = routeErrorComponent
115
+ ? CatchBoundary
116
+ : SafeFragment
117
+
118
+ return (
119
+ <matchContext.Provider value={matchId}>
120
+ <ResolvedSuspenseBoundary fallback={pendingElement}>
121
+ <ResolvedCatchBoundary
122
+ getResetKey={() => router.state.resolvedLocation.state?.key}
123
+ errorComponent={routeErrorComponent}
124
+ onCatch={() => {
125
+ warning(false, `Error in route match: ${matchId}`)
126
+ }}
127
+ >
128
+ <MatchInner matchId={matchId!} pendingElement={pendingElement} />
129
+ </ResolvedCatchBoundary>
130
+ </ResolvedSuspenseBoundary>
131
+ </matchContext.Provider>
132
+ )
133
+ }
134
+
135
+ function MatchInner({
136
+ matchId,
137
+ pendingElement,
138
+ }: {
139
+ matchId: string
140
+ pendingElement: any
141
+ }): any {
142
+ const router = useRouter()
143
+ const routeId = useRouterState({
144
+ select: (s) =>
145
+ getRenderedMatches(s).find((d) => d.id === matchId)?.routeId as string,
146
+ })
147
+
148
+ const route = router.routesById[routeId]!
149
+
150
+ const match = useRouterState({
151
+ select: (s) =>
152
+ pick(getRenderedMatches(s).find((d) => d.id === matchId)!, [
153
+ 'status',
154
+ 'error',
155
+ 'showPending',
156
+ 'loadPromise',
157
+ ]),
158
+ })
159
+
160
+ if (match.status === 'error') {
161
+ throw match.error
162
+ }
163
+
164
+ if (match.status === 'pending') {
165
+ if (match.showPending) {
166
+ return pendingElement
167
+ }
168
+ throw match.loadPromise
169
+ }
170
+
171
+ if (match.status === 'success') {
172
+ let Comp = route.options.component ?? router.options.defaultComponent
173
+
174
+ if (Comp) {
175
+ return <Comp />
176
+ }
177
+
178
+ return <Outlet />
179
+ }
180
+
181
+ invariant(
182
+ false,
183
+ 'Idle routeMatch status encountered during rendering! You should never see this. File an issue!',
184
+ )
185
+ }
186
+
187
+ export const Outlet = React.memo(function Outlet() {
188
+ const matchId = React.useContext(matchContext)
189
+
190
+ const childMatchId = useRouterState({
191
+ select: (s) => {
192
+ const matches = getRenderedMatches(s)
193
+ const index = matches.findIndex((d) => d.id === matchId)
194
+ return matches[index + 1]?.id
195
+ },
196
+ })
197
+
198
+ if (!childMatchId) {
199
+ return null
200
+ }
201
+
202
+ return <Match matchId={childMatchId} />
203
+ })
204
+
205
+ export interface MatchRouteOptions {
206
+ pending?: boolean
207
+ caseSensitive?: boolean
208
+ includeSearch?: boolean
209
+ fuzzy?: boolean
210
+ }
211
+
212
+ export type UseMatchRouteOptions<
213
+ TRouteTree extends AnyRoute = RegisteredRouter['routeTree'],
214
+ TFrom extends RoutePaths<TRouteTree> = '/',
215
+ TTo extends string = '',
216
+ TMaskFrom extends RoutePaths<TRouteTree> = '/',
217
+ TMaskTo extends string = '',
218
+ > = ToOptions<AnyRoute, TFrom, TTo, TMaskFrom, TMaskTo> & MatchRouteOptions
219
+
220
+ export function useMatchRoute<
221
+ TRouteTree extends AnyRoute = RegisteredRouter['routeTree'],
222
+ >() {
223
+ useRouterState({ select: (s) => [s.location, s.resolvedLocation] })
224
+ const { matchRoute } = useRouter()
225
+
226
+ return React.useCallback(
227
+ <
228
+ TFrom extends RoutePaths<TRouteTree> = '/',
229
+ TTo extends string = '',
230
+ TMaskFrom extends RoutePaths<TRouteTree> = '/',
231
+ TMaskTo extends string = '',
232
+ TResolved extends string = ResolveRelativePath<TFrom, NoInfer<TTo>>,
233
+ >(
234
+ opts: UseMatchRouteOptions<TRouteTree, TFrom, TTo, TMaskFrom, TMaskTo>,
235
+ ): false | RouteById<TRouteTree, TResolved>['types']['allParams'] => {
236
+ const { pending, caseSensitive, ...rest } = opts
237
+
238
+ return matchRoute(rest as any, {
239
+ pending,
240
+ caseSensitive,
241
+ })
242
+ },
243
+ [],
244
+ )
245
+ }
246
+
247
+ export type MakeMatchRouteOptions<
248
+ TRouteTree extends AnyRoute = RegisteredRouter['routeTree'],
249
+ TFrom extends RoutePaths<TRouteTree> = '/',
250
+ TTo extends string = '',
251
+ TMaskFrom extends RoutePaths<TRouteTree> = '/',
252
+ TMaskTo extends string = '',
253
+ > = ToOptions<TRouteTree, TFrom, TTo, TMaskFrom, TMaskTo> &
254
+ MatchRouteOptions & {
255
+ // If a function is passed as a child, it will be given the `isActive` boolean to aid in further styling on the element it returns
256
+ children?:
257
+ | ((
258
+ params?: RouteByPath<
259
+ TRouteTree,
260
+ ResolveRelativePath<TFrom, NoInfer<TTo>>
261
+ >['types']['allParams'],
262
+ ) => ReactNode)
263
+ | React.ReactNode
264
+ }
265
+
266
+ export function MatchRoute<
267
+ TRouteTree extends AnyRoute = RegisteredRouter['routeTree'],
268
+ TFrom extends RoutePaths<TRouteTree> = '/',
269
+ TTo extends string = '',
270
+ TMaskFrom extends RoutePaths<TRouteTree> = '/',
271
+ TMaskTo extends string = '',
272
+ >(
273
+ props: MakeMatchRouteOptions<TRouteTree, TFrom, TTo, TMaskFrom, TMaskTo>,
274
+ ): any {
275
+ const matchRoute = useMatchRoute()
276
+ const params = matchRoute(props as any)
277
+
278
+ if (typeof props.children === 'function') {
279
+ return (props.children as any)(params)
280
+ }
281
+
282
+ return !!params ? props.children : null
283
+ }
284
+
285
+ function getRenderedMatches(state: RouterState) {
286
+ return state.pendingMatches?.some((d) => d.showPending)
287
+ ? state.pendingMatches
288
+ : state.matches
289
+ }
290
+
291
+ export function useMatch<
292
+ TRouteTree extends AnyRoute = RegisteredRouter['routeTree'],
293
+ TFrom extends RouteIds<TRouteTree> = RouteIds<TRouteTree>,
294
+ TStrict extends boolean = true,
295
+ TRouteMatchState = RouteMatch<TRouteTree, TFrom>,
296
+ TSelected = TRouteMatchState,
297
+ >(
298
+ opts: StrictOrFrom<TFrom> & {
299
+ select?: (match: TRouteMatchState) => TSelected
300
+ },
301
+ ): TStrict extends true ? TSelected : TSelected | undefined {
302
+ const router = useRouter()
303
+ const nearestMatchId = React.useContext(matchContext)
304
+
305
+ const nearestMatchRouteId = getRenderedMatches(router.state).find(
306
+ (d) => d.id === nearestMatchId,
307
+ )?.routeId
308
+
309
+ const matchRouteId = (() => {
310
+ const matches = getRenderedMatches(router.state)
311
+ const match = opts?.from
312
+ ? matches.find((d) => d.routeId === opts?.from)
313
+ : matches.find((d) => d.id === nearestMatchId)
314
+ return match!.routeId
315
+ })()
316
+
317
+ if (opts?.strict ?? true) {
318
+ invariant(
319
+ nearestMatchRouteId == matchRouteId,
320
+ `useMatch("${
321
+ matchRouteId as string
322
+ }") is being called in a component that is meant to render the '${nearestMatchRouteId}' route. Did you mean to 'useMatch("${
323
+ matchRouteId as string
324
+ }", { strict: false })' or 'useRoute("${
325
+ matchRouteId as string
326
+ }")' instead?`,
327
+ )
328
+ }
329
+
330
+ const matchSelection = useRouterState({
331
+ select: (state) => {
332
+ const match = getRenderedMatches(state).find(
333
+ (d) => d.id === nearestMatchId,
334
+ )
335
+
336
+ invariant(
337
+ match,
338
+ `Could not find ${
339
+ opts?.from
340
+ ? `an active match from "${opts.from}"`
341
+ : 'a nearest match!'
342
+ }`,
343
+ )
344
+
345
+ return opts?.select ? opts.select(match as any) : match
346
+ },
347
+ })
348
+
349
+ return matchSelection as any
350
+ }
351
+
352
+ export function useMatches<T = RouteMatch[]>(opts?: {
353
+ select?: (matches: RouteMatch[]) => T
354
+ }): T {
355
+ return useRouterState({
356
+ select: (state) => {
357
+ let matches = getRenderedMatches(state)
358
+ return opts?.select ? opts.select(matches) : (matches as T)
359
+ },
360
+ })
361
+ }
362
+
363
+ export function useParentMatches<T = RouteMatch[]>(opts?: {
364
+ select?: (matches: RouteMatch[]) => T
365
+ }): T {
366
+ const contextMatchId = React.useContext(matchContext)
367
+
368
+ return useMatches({
369
+ select: (matches) => {
370
+ matches = matches.slice(matches.findIndex((d) => d.id === contextMatchId))
371
+ return opts?.select ? opts.select(matches) : (matches as T)
372
+ },
373
+ })
374
+ }
375
+
376
+ export function useLoaderDeps<
377
+ TRouteTree extends AnyRoute = RegisteredRouter['routeTree'],
378
+ TFrom extends RouteIds<TRouteTree> = RouteIds<TRouteTree>,
379
+ TStrict extends boolean = true,
380
+ TRouteMatch extends RouteMatch<TRouteTree, TFrom> = RouteMatch<
381
+ TRouteTree,
382
+ TFrom
383
+ >,
384
+ TSelected = Required<TRouteMatch>['loaderDeps'],
385
+ >(
386
+ opts: StrictOrFrom<TFrom> & {
387
+ select?: (match: TRouteMatch) => TSelected
388
+ },
389
+ ): TStrict extends true ? TSelected : TSelected | undefined {
390
+ return useMatch({
391
+ ...opts,
392
+ select: (s) => {
393
+ return typeof opts.select === 'function'
394
+ ? opts.select(s?.loaderDeps)
395
+ : s?.loaderDeps
396
+ },
397
+ })!
398
+ }
399
+
400
+ export function useLoaderData<
401
+ TRouteTree extends AnyRoute = RegisteredRouter['routeTree'],
402
+ TFrom extends RouteIds<TRouteTree> = RouteIds<TRouteTree>,
403
+ TStrict extends boolean = true,
404
+ TRouteMatch extends RouteMatch<TRouteTree, TFrom> = RouteMatch<
405
+ TRouteTree,
406
+ TFrom
407
+ >,
408
+ TSelected = Required<TRouteMatch>['loaderData'],
409
+ >(
410
+ opts: StrictOrFrom<TFrom> & {
411
+ select?: (match: TRouteMatch) => TSelected
412
+ },
413
+ ): TStrict extends true ? TSelected : TSelected | undefined {
414
+ return useMatch({
415
+ ...opts,
416
+ select: (s) => {
417
+ return typeof opts.select === 'function'
418
+ ? opts.select(s?.loaderData)
419
+ : s?.loaderData
420
+ },
421
+ })!
422
+ }