@tanstack/react-router 0.0.1-beta.214 → 0.0.1-beta.216

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.
package/src/route.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import { HistoryLocation } from '@tanstack/history'
2
2
  import * as React from 'react'
3
3
  import invariant from 'tiny-invariant'
4
- import { useMatch } from './Matches'
4
+ import { useLoaderData, useMatch } from './Matches'
5
5
  import { AnyRouteMatch } from './RouterProvider'
6
6
  import { NavigateOptions, ParsePathParams, ToSubOptions } from './link'
7
7
  import { ParsedLocation } from './location'
@@ -63,6 +63,7 @@ export type RouteOptions<
63
63
  TAllParams extends AnyPathParams = TParams,
64
64
  TRouteContext extends RouteContext = RouteContext,
65
65
  TAllContext extends Record<string, any> = AnyContext,
66
+ TLoaderData extends any = unknown,
66
67
  > = BaseRouteOptions<
67
68
  TParentRoute,
68
69
  TCustomId,
@@ -72,9 +73,17 @@ export type RouteOptions<
72
73
  TParams,
73
74
  TAllParams,
74
75
  TRouteContext,
75
- TAllContext
76
+ TAllContext,
77
+ TLoaderData
76
78
  > &
77
- NoInfer<UpdatableRouteOptions<TFullSearchSchema, TAllParams, TAllContext>>
79
+ NoInfer<
80
+ UpdatableRouteOptions<
81
+ TFullSearchSchema,
82
+ TAllParams,
83
+ TAllContext,
84
+ TLoaderData
85
+ >
86
+ >
78
87
 
79
88
  export type ParamsFallback<
80
89
  TPath extends string,
@@ -95,9 +104,18 @@ export type BaseRouteOptions<
95
104
  TAllParams = ParamsFallback<TPath, TParams>,
96
105
  TRouteContext extends RouteContext = RouteContext,
97
106
  TAllContext extends Record<string, any> = AnyContext,
107
+ TLoaderData extends any = unknown,
98
108
  > = RoutePathOptions<TCustomId, TPath> & {
99
109
  getParentRoute: () => TParentRoute
100
110
  validateSearch?: SearchSchemaValidator<TSearchSchema>
111
+ shouldReload?: (
112
+ match: LoaderFnContext<
113
+ TAllParams,
114
+ TFullSearchSchema,
115
+ TAllContext,
116
+ TRouteContext
117
+ >,
118
+ ) => any
101
119
  } & (keyof PickRequired<RouteContext> extends never
102
120
  ? // This async function is called before a route is loaded.
103
121
  // If an error is thrown here, the route's loader will not be called.
@@ -119,11 +137,12 @@ export type BaseRouteOptions<
119
137
  TRouteContext
120
138
  >
121
139
  }) & {
122
- load?: RouteLoadFn<
140
+ loader?: RouteLoadFn<
123
141
  TAllParams,
124
142
  TFullSearchSchema,
125
143
  NoInfer<TAllContext>,
126
- NoInfer<TRouteContext>
144
+ NoInfer<TRouteContext>,
145
+ TLoaderData
127
146
  >
128
147
  } & (
129
148
  | {
@@ -163,6 +182,7 @@ export type UpdatableRouteOptions<
163
182
  TFullSearchSchema extends Record<string, any>,
164
183
  TAllParams extends AnyPathParams,
165
184
  TAllContext extends AnyContext,
185
+ TLoaderData extends any = unknown,
166
186
  > = MetaOptions & {
167
187
  // test?: (args: TAllContext) => void
168
188
  // If true, this route will be matched as case-sensitive
@@ -170,7 +190,12 @@ export type UpdatableRouteOptions<
170
190
  // If true, this route will be forcefully wrapped in a suspense boundary
171
191
  wrapInSuspense?: boolean
172
192
  // The content to be rendered when the route is matched. If no component is provided, defaults to `<Outlet />`
173
- component?: RouteComponent<TFullSearchSchema, TAllParams, TAllContext>
193
+ component?: RouteComponent<
194
+ TFullSearchSchema,
195
+ TAllParams,
196
+ TAllContext,
197
+ TLoaderData
198
+ >
174
199
  // The content to be rendered when the route encounters an error
175
200
  errorComponent?: ErrorRouteComponent<
176
201
  TFullSearchSchema,
@@ -244,18 +269,17 @@ export type RouteLoadFn<
244
269
  TFullSearchSchema extends Record<string, any> = {},
245
270
  TAllContext extends Record<string, any> = AnyContext,
246
271
  TRouteContext extends Record<string, any> = AnyContext,
272
+ TLoaderData extends any = unknown,
247
273
  > = (
248
- match: LoadFnContext<
274
+ match: LoaderFnContext<
249
275
  TAllParams,
250
276
  TFullSearchSchema,
251
277
  TAllContext,
252
278
  TRouteContext
253
- > & {
254
- parentMatchPromise?: Promise<void>
255
- },
256
- ) => any
279
+ >,
280
+ ) => Promise<TLoaderData> | TLoaderData
257
281
 
258
- export interface LoadFnContext<
282
+ export interface LoaderFnContext<
259
283
  TAllParams = {},
260
284
  TFullSearchSchema extends Record<string, any> = {},
261
285
  TAllContext extends Record<string, any> = AnyContext,
@@ -268,6 +292,8 @@ export interface LoadFnContext<
268
292
  context: Expand<Assign<TAllContext, TRouteContext>>
269
293
  location: ParsedLocation<TFullSearchSchema>
270
294
  navigate: (opts: NavigateOptions<AnyRoute>) => Promise<void>
295
+ parentMatchPromise?: Promise<void>
296
+ cause: 'enter' | 'stay'
271
297
  }
272
298
 
273
299
  export type SearchFilter<T, U = T> = (prev: T) => U
@@ -378,6 +404,7 @@ export class Route<
378
404
  Assign<IsAny<TParentRoute['types']['allContext'], {}>, TRouteContext>
379
405
  >,
380
406
  TRouterContext extends RouteConstraints['TRouterContext'] = AnyContext,
407
+ TLoaderData extends any = unknown,
381
408
  TChildren extends RouteConstraints['TChildren'] = unknown,
382
409
  TRouteTree extends RouteConstraints['TRouteTree'] = AnyRoute,
383
410
  > {
@@ -391,7 +418,8 @@ export class Route<
391
418
  TParams,
392
419
  TAllParams,
393
420
  TRouteContext,
394
- TAllContext
421
+ TAllContext,
422
+ TLoaderData
395
423
  >
396
424
 
397
425
  test!: Expand<
@@ -422,7 +450,8 @@ export class Route<
422
450
  TParams,
423
451
  TAllParams,
424
452
  TRouteContext,
425
- TAllContext
453
+ TAllContext,
454
+ TLoaderData
426
455
  >,
427
456
  ) {
428
457
  this.options = (options as any) || {}
@@ -446,6 +475,7 @@ export class Route<
446
475
  children: TChildren
447
476
  routeTree: TRouteTree
448
477
  routerContext: TRouterContext
478
+ loaderData: TLoaderData
449
479
  }
450
480
 
451
481
  init = (opts: { originalIndex: number }) => {
@@ -460,7 +490,8 @@ export class Route<
460
490
  TParams,
461
491
  TAllParams,
462
492
  TRouteContext,
463
- TAllContext
493
+ TAllContext,
494
+ TLoaderData
464
495
  > &
465
496
  RoutePathOptionsIntersection<TCustomId, TPath>
466
497
 
@@ -542,7 +573,8 @@ export class Route<
542
573
  TAllParams,
543
574
  Expand<
544
575
  Assign<IsAny<TParentRoute['types']['allContext'], {}>, TRouteContext>
545
- >
576
+ >,
577
+ TLoaderData
546
578
  >,
547
579
  ) => {
548
580
  Object.assign(this.options, options)
@@ -578,6 +610,11 @@ export class Route<
578
610
  }): TSelected => {
579
611
  return useParams({ ...opts, from: this.id } as any)
580
612
  }
613
+ useLoaderData = <TSelected = TLoaderData>(opts?: {
614
+ select?: (search: TLoaderData) => TSelected
615
+ }): TSelected => {
616
+ return useLoaderData({ ...opts, from: this.id } as any) as any
617
+ }
581
618
  }
582
619
 
583
620
  export type AnyRootRoute = RootRoute<any, any, any>
@@ -586,6 +623,7 @@ export function rootRouteWithContext<TRouterContext extends {}>() {
586
623
  return <
587
624
  TSearchSchema extends Record<string, any> = {},
588
625
  TRouteContext extends RouteContext = RouteContext,
626
+ TLoaderData extends any = unknown,
589
627
  >(
590
628
  options?: Omit<
591
629
  RouteOptions<
@@ -597,7 +635,8 @@ export function rootRouteWithContext<TRouterContext extends {}>() {
597
635
  {}, // TParams
598
636
  {}, // TAllParams
599
637
  TRouteContext, // TRouteContext
600
- Assign<TRouterContext, TRouteContext> // TAllContext
638
+ Assign<TRouterContext, TRouteContext>, // TAllContext
639
+ TLoaderData // TLoaderData
601
640
  >,
602
641
  | 'path'
603
642
  | 'id'
@@ -615,6 +654,7 @@ export class RootRoute<
615
654
  TSearchSchema extends Record<string, any> = {},
616
655
  TRouteContext extends RouteContext = RouteContext,
617
656
  TRouterContext extends {} = {},
657
+ TLoaderData extends any = unknown,
618
658
  > extends Route<
619
659
  any, // TParentRoute
620
660
  '/', // TPath
@@ -628,6 +668,7 @@ export class RootRoute<
628
668
  TRouteContext, // TRouteContext
629
669
  Expand<Assign<TRouterContext, TRouteContext>>, // TAllContext
630
670
  TRouterContext, // TRouterContext
671
+ TLoaderData,
631
672
  any, // TChildren
632
673
  any // TRouteTree
633
674
  > {
@@ -642,7 +683,8 @@ export class RootRoute<
642
683
  {}, // TParams
643
684
  {}, // TAllParams
644
685
  TRouteContext, // TRouteContext
645
- Assign<TRouterContext, TRouteContext> // TAllContext
686
+ Assign<TRouterContext, TRouteContext>, // TAllContext
687
+ TLoaderData
646
688
  >,
647
689
  | 'path'
648
690
  | 'id'
@@ -720,6 +762,7 @@ export type RouteProps<
720
762
  TFullSearchSchema extends Record<string, any> = AnySearchSchema,
721
763
  TAllParams extends AnyPathParams = AnyPathParams,
722
764
  TAllContext extends Record<string, any> = AnyContext,
765
+ TLoaderData extends any = unknown,
723
766
  > = {
724
767
  useMatch: <TSelected = TAllContext>(opts?: {
725
768
  select?: (search: TAllContext) => TSelected
@@ -733,6 +776,9 @@ export type RouteProps<
733
776
  useParams: <TSelected = TAllParams>(opts?: {
734
777
  select?: (search: TAllParams) => TSelected
735
778
  }) => TSelected
779
+ useLoaderData: <TSelected = TLoaderData>(opts?: {
780
+ select?: (search: TLoaderData) => TSelected
781
+ }) => TSelected
736
782
  }
737
783
 
738
784
  export type ErrorRouteProps<
@@ -765,7 +811,10 @@ export type RouteComponent<
765
811
  TFullSearchSchema extends Record<string, any>,
766
812
  TAllParams extends AnyPathParams,
767
813
  TAllContext extends Record<string, any>,
768
- > = AsyncRouteComponent<RouteProps<TFullSearchSchema, TAllParams, TAllContext>>
814
+ TLoaderData extends any = unknown,
815
+ > = AsyncRouteComponent<
816
+ RouteProps<TFullSearchSchema, TAllParams, TAllContext, TLoaderData>
817
+ >
769
818
 
770
819
  export type ErrorRouteComponent<
771
820
  TFullSearchSchema extends Record<string, any>,
@@ -783,4 +832,4 @@ export type PendingRouteComponent<
783
832
  PendingRouteProps<TFullSearchSchema, TAllParams, TAllContext>
784
833
  >
785
834
 
786
- export type AnyRouteComponent = RouteComponent<any, any, any>
835
+ export type AnyRouteComponent = RouteComponent<any, any, any, any>
package/src/router.ts CHANGED
@@ -36,7 +36,7 @@ export interface Register {
36
36
  // router: Router
37
37
  }
38
38
 
39
- export type AnyRouter = Router<any, any>
39
+ export type AnyRouter = Router<AnyRoute, any>
40
40
 
41
41
  export type RegisteredRouter = Register extends {
42
42
  router: infer TRouter extends AnyRouter