@tanstack/react-router 0.0.1-beta.27 → 0.0.1-beta.270

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