@tanstack/react-router 0.0.1-beta.26 → 0.0.1-beta.261

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