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

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 +1049 -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 +2255 -2571
  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 +62 -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 +282 -0
  65. package/build/types/routeInfo.d.ts +22 -0
  66. package/build/types/router.d.ts +188 -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 +2858 -2548
  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 +421 -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 +872 -0
  94. package/src/routeInfo.ts +70 -0
  95. package/src/router.ts +1630 -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.280",
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.280"
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,421 @@
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
+ }
47
+
48
+ export type AnyRouteMatch = RouteMatch<any, any>
49
+
50
+ export function Matches() {
51
+ const router = useRouter()
52
+ const matchId = useRouterState({
53
+ select: (s) => {
54
+ return getRenderedMatches(s)[0]?.id
55
+ },
56
+ })
57
+
58
+ return (
59
+ <matchContext.Provider value={matchId}>
60
+ <CatchBoundary
61
+ getResetKey={() => router.state.resolvedLocation.state?.key}
62
+ errorComponent={ErrorComponent}
63
+ onCatch={() => {
64
+ warning(
65
+ false,
66
+ `Error in router! Consider setting an 'errorComponent' in your RootRoute! 👍`,
67
+ )
68
+ }}
69
+ >
70
+ {matchId ? <Match matchId={matchId} /> : null}
71
+ </CatchBoundary>
72
+ </matchContext.Provider>
73
+ )
74
+ }
75
+
76
+ function SafeFragment(props: any) {
77
+ return <>{props.children}</>
78
+ }
79
+
80
+ export function Match({ matchId }: { matchId: string }) {
81
+ const router = useRouter()
82
+ const routeId = useRouterState({
83
+ select: (s) =>
84
+ getRenderedMatches(s).find((d) => d.id === matchId)?.routeId as string,
85
+ })
86
+
87
+ invariant(
88
+ routeId,
89
+ `Could not find routeId for matchId "${matchId}". Please file an issue!`,
90
+ )
91
+
92
+ const route = router.routesById[routeId]!
93
+
94
+ const PendingComponent = (route.options.pendingComponent ??
95
+ router.options.defaultPendingComponent) as any
96
+
97
+ const pendingElement = PendingComponent ? <PendingComponent /> : null
98
+
99
+ const routeErrorComponent =
100
+ route.options.errorComponent ??
101
+ router.options.defaultErrorComponent ??
102
+ ErrorComponent
103
+
104
+ const ResolvedSuspenseBoundary =
105
+ route.options.wrapInSuspense ??
106
+ PendingComponent ??
107
+ route.options.component?.preload ??
108
+ route.options.pendingComponent?.preload ??
109
+ (route.options.errorComponent as any)?.preload
110
+ ? React.Suspense
111
+ : SafeFragment
112
+
113
+ const ResolvedCatchBoundary = routeErrorComponent
114
+ ? CatchBoundary
115
+ : SafeFragment
116
+
117
+ return (
118
+ <matchContext.Provider value={matchId}>
119
+ <ResolvedSuspenseBoundary fallback={pendingElement}>
120
+ <ResolvedCatchBoundary
121
+ getResetKey={() => router.state.resolvedLocation.state?.key}
122
+ errorComponent={routeErrorComponent}
123
+ onCatch={() => {
124
+ warning(false, `Error in route match: ${matchId}`)
125
+ }}
126
+ >
127
+ <MatchInner matchId={matchId!} pendingElement={pendingElement} />
128
+ </ResolvedCatchBoundary>
129
+ </ResolvedSuspenseBoundary>
130
+ </matchContext.Provider>
131
+ )
132
+ }
133
+
134
+ function MatchInner({
135
+ matchId,
136
+ pendingElement,
137
+ }: {
138
+ matchId: string
139
+ pendingElement: any
140
+ }): any {
141
+ const router = useRouter()
142
+ const routeId = useRouterState({
143
+ select: (s) =>
144
+ getRenderedMatches(s).find((d) => d.id === matchId)?.routeId as string,
145
+ })
146
+
147
+ const route = router.routesById[routeId]!
148
+
149
+ const match = useRouterState({
150
+ select: (s) =>
151
+ pick(getRenderedMatches(s).find((d) => d.id === matchId)!, [
152
+ 'status',
153
+ 'error',
154
+ 'showPending',
155
+ 'loadPromise',
156
+ ]),
157
+ })
158
+
159
+ if (match.status === 'error') {
160
+ throw match.error
161
+ }
162
+
163
+ if (match.status === 'pending') {
164
+ if (match.showPending) {
165
+ return pendingElement
166
+ }
167
+ throw match.loadPromise
168
+ }
169
+
170
+ if (match.status === 'success') {
171
+ let Comp = route.options.component ?? router.options.defaultComponent
172
+
173
+ if (Comp) {
174
+ return <Comp />
175
+ }
176
+
177
+ return <Outlet />
178
+ }
179
+
180
+ invariant(
181
+ false,
182
+ 'Idle routeMatch status encountered during rendering! You should never see this. File an issue!',
183
+ )
184
+ }
185
+
186
+ export const Outlet = React.memo(function Outlet() {
187
+ const matchId = React.useContext(matchContext)
188
+
189
+ const childMatchId = useRouterState({
190
+ select: (s) => {
191
+ const matches = getRenderedMatches(s)
192
+ const index = matches.findIndex((d) => d.id === matchId)
193
+ return matches[index + 1]?.id
194
+ },
195
+ })
196
+
197
+ if (!childMatchId) {
198
+ return null
199
+ }
200
+
201
+ return <Match matchId={childMatchId} />
202
+ })
203
+
204
+ export interface MatchRouteOptions {
205
+ pending?: boolean
206
+ caseSensitive?: boolean
207
+ includeSearch?: boolean
208
+ fuzzy?: boolean
209
+ }
210
+
211
+ export type UseMatchRouteOptions<
212
+ TRouteTree extends AnyRoute = RegisteredRouter['routeTree'],
213
+ TFrom extends RoutePaths<TRouteTree> = '/',
214
+ TTo extends string = '',
215
+ TMaskFrom extends RoutePaths<TRouteTree> = '/',
216
+ TMaskTo extends string = '',
217
+ > = ToOptions<AnyRoute, TFrom, TTo, TMaskFrom, TMaskTo> & MatchRouteOptions
218
+
219
+ export function useMatchRoute<
220
+ TRouteTree extends AnyRoute = RegisteredRouter['routeTree'],
221
+ >() {
222
+ useRouterState({ select: (s) => [s.location, s.resolvedLocation] })
223
+ const { matchRoute } = useRouter()
224
+
225
+ return React.useCallback(
226
+ <
227
+ TFrom extends RoutePaths<TRouteTree> = '/',
228
+ TTo extends string = '',
229
+ TMaskFrom extends RoutePaths<TRouteTree> = '/',
230
+ TMaskTo extends string = '',
231
+ TResolved extends string = ResolveRelativePath<TFrom, NoInfer<TTo>>,
232
+ >(
233
+ opts: UseMatchRouteOptions<TRouteTree, TFrom, TTo, TMaskFrom, TMaskTo>,
234
+ ): false | RouteById<TRouteTree, TResolved>['types']['allParams'] => {
235
+ const { pending, caseSensitive, ...rest } = opts
236
+
237
+ return matchRoute(rest as any, {
238
+ pending,
239
+ caseSensitive,
240
+ })
241
+ },
242
+ [],
243
+ )
244
+ }
245
+
246
+ export type MakeMatchRouteOptions<
247
+ TRouteTree extends AnyRoute = RegisteredRouter['routeTree'],
248
+ TFrom extends RoutePaths<TRouteTree> = '/',
249
+ TTo extends string = '',
250
+ TMaskFrom extends RoutePaths<TRouteTree> = '/',
251
+ TMaskTo extends string = '',
252
+ > = ToOptions<TRouteTree, TFrom, TTo, TMaskFrom, TMaskTo> &
253
+ MatchRouteOptions & {
254
+ // 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
255
+ children?:
256
+ | ((
257
+ params?: RouteByPath<
258
+ TRouteTree,
259
+ ResolveRelativePath<TFrom, NoInfer<TTo>>
260
+ >['types']['allParams'],
261
+ ) => ReactNode)
262
+ | React.ReactNode
263
+ }
264
+
265
+ export function MatchRoute<
266
+ TRouteTree extends AnyRoute = RegisteredRouter['routeTree'],
267
+ TFrom extends RoutePaths<TRouteTree> = '/',
268
+ TTo extends string = '',
269
+ TMaskFrom extends RoutePaths<TRouteTree> = '/',
270
+ TMaskTo extends string = '',
271
+ >(
272
+ props: MakeMatchRouteOptions<TRouteTree, TFrom, TTo, TMaskFrom, TMaskTo>,
273
+ ): any {
274
+ const matchRoute = useMatchRoute()
275
+ const params = matchRoute(props as any)
276
+
277
+ if (typeof props.children === 'function') {
278
+ return (props.children as any)(params)
279
+ }
280
+
281
+ return !!params ? props.children : null
282
+ }
283
+
284
+ function getRenderedMatches(state: RouterState) {
285
+ return state.pendingMatches?.some((d) => d.showPending)
286
+ ? state.pendingMatches
287
+ : state.matches
288
+ }
289
+
290
+ export function useMatch<
291
+ TRouteTree extends AnyRoute = RegisteredRouter['routeTree'],
292
+ TFrom extends RouteIds<TRouteTree> = RouteIds<TRouteTree>,
293
+ TStrict extends boolean = true,
294
+ TRouteMatchState = RouteMatch<TRouteTree, TFrom>,
295
+ TSelected = TRouteMatchState,
296
+ >(
297
+ opts: StrictOrFrom<TFrom> & {
298
+ select?: (match: TRouteMatchState) => TSelected
299
+ },
300
+ ): TStrict extends true ? TSelected : TSelected | undefined {
301
+ const router = useRouter()
302
+ const nearestMatchId = React.useContext(matchContext)
303
+
304
+ const nearestMatchRouteId = getRenderedMatches(router.state).find(
305
+ (d) => d.id === nearestMatchId,
306
+ )?.routeId
307
+
308
+ const matchRouteId = (() => {
309
+ const matches = getRenderedMatches(router.state)
310
+ const match = opts?.from
311
+ ? matches.find((d) => d.routeId === opts?.from)
312
+ : matches.find((d) => d.id === nearestMatchId)
313
+ return match!.routeId
314
+ })()
315
+
316
+ if (opts?.strict ?? true) {
317
+ invariant(
318
+ nearestMatchRouteId == matchRouteId,
319
+ `useMatch("${
320
+ matchRouteId as string
321
+ }") is being called in a component that is meant to render the '${nearestMatchRouteId}' route. Did you mean to 'useMatch("${
322
+ matchRouteId as string
323
+ }", { strict: false })' or 'useRoute("${
324
+ matchRouteId as string
325
+ }")' instead?`,
326
+ )
327
+ }
328
+
329
+ const matchSelection = useRouterState({
330
+ select: (state) => {
331
+ const match = getRenderedMatches(state).find(
332
+ (d) => d.id === nearestMatchId,
333
+ )
334
+
335
+ invariant(
336
+ match,
337
+ `Could not find ${
338
+ opts?.from
339
+ ? `an active match from "${opts.from}"`
340
+ : 'a nearest match!'
341
+ }`,
342
+ )
343
+
344
+ return opts?.select ? opts.select(match as any) : match
345
+ },
346
+ })
347
+
348
+ return matchSelection as any
349
+ }
350
+
351
+ export function useMatches<T = RouteMatch[]>(opts?: {
352
+ select?: (matches: RouteMatch[]) => T
353
+ }): T {
354
+ return useRouterState({
355
+ select: (state) => {
356
+ let matches = getRenderedMatches(state)
357
+ return opts?.select ? opts.select(matches) : (matches as T)
358
+ },
359
+ })
360
+ }
361
+
362
+ export function useParentMatches<T = RouteMatch[]>(opts?: {
363
+ select?: (matches: RouteMatch[]) => T
364
+ }): T {
365
+ const contextMatchId = React.useContext(matchContext)
366
+
367
+ return useMatches({
368
+ select: (matches) => {
369
+ matches = matches.slice(matches.findIndex((d) => d.id === contextMatchId))
370
+ return opts?.select ? opts.select(matches) : (matches as T)
371
+ },
372
+ })
373
+ }
374
+
375
+ export function useLoaderDeps<
376
+ TRouteTree extends AnyRoute = RegisteredRouter['routeTree'],
377
+ TFrom extends RouteIds<TRouteTree> = RouteIds<TRouteTree>,
378
+ TStrict extends boolean = true,
379
+ TRouteMatch extends RouteMatch<TRouteTree, TFrom> = RouteMatch<
380
+ TRouteTree,
381
+ TFrom
382
+ >,
383
+ TSelected = Required<TRouteMatch>['loaderDeps'],
384
+ >(
385
+ opts: StrictOrFrom<TFrom> & {
386
+ select?: (match: TRouteMatch) => TSelected
387
+ },
388
+ ): TStrict extends true ? TSelected : TSelected | undefined {
389
+ return useMatch({
390
+ ...opts,
391
+ select: (s) => {
392
+ return typeof opts.select === 'function'
393
+ ? opts.select(s?.loaderDeps)
394
+ : s?.loaderDeps
395
+ },
396
+ })!
397
+ }
398
+
399
+ export function useLoaderData<
400
+ TRouteTree extends AnyRoute = RegisteredRouter['routeTree'],
401
+ TFrom extends RouteIds<TRouteTree> = RouteIds<TRouteTree>,
402
+ TStrict extends boolean = true,
403
+ TRouteMatch extends RouteMatch<TRouteTree, TFrom> = RouteMatch<
404
+ TRouteTree,
405
+ TFrom
406
+ >,
407
+ TSelected = Required<TRouteMatch>['loaderData'],
408
+ >(
409
+ opts: StrictOrFrom<TFrom> & {
410
+ select?: (match: TRouteMatch) => TSelected
411
+ },
412
+ ): TStrict extends true ? TSelected : TSelected | undefined {
413
+ return useMatch({
414
+ ...opts,
415
+ select: (s) => {
416
+ return typeof opts.select === 'function'
417
+ ? opts.select(s?.loaderData)
418
+ : s?.loaderData
419
+ },
420
+ })!
421
+ }