@tanstack/react-router 0.0.1-beta.207 → 0.0.1-beta.209
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/build/cjs/RouterProvider.js +25 -15
- package/build/cjs/RouterProvider.js.map +1 -1
- package/build/cjs/fileRoute.js.map +1 -1
- package/build/cjs/index.js +3 -2
- package/build/cjs/index.js.map +1 -1
- package/build/cjs/react.js +11 -7
- package/build/cjs/react.js.map +1 -1
- package/build/cjs/route.js +6 -13
- package/build/cjs/route.js.map +1 -1
- package/build/cjs/router.js +1 -1
- package/build/cjs/router.js.map +1 -1
- package/build/esm/index.js +41 -35
- package/build/esm/index.js.map +1 -1
- package/build/stats-html.html +1 -1
- package/build/stats-react.json +171 -171
- package/build/types/RouteMatch.d.ts +1 -1
- package/build/types/fileRoute.d.ts +17 -2
- package/build/types/react.d.ts +5 -4
- package/build/types/route.d.ts +46 -39
- package/build/types/router.d.ts +5 -5
- package/build/umd/index.development.js +43 -36
- package/build/umd/index.development.js.map +1 -1
- package/build/umd/index.production.js +1 -1
- package/build/umd/index.production.js.map +1 -1
- package/package.json +2 -2
- package/src/RouteMatch.ts +1 -1
- package/src/RouterProvider.tsx +19 -15
- package/src/fileRoute.ts +10 -8
- package/src/react.tsx +27 -11
- package/src/route.ts +112 -106
- package/src/router.ts +6 -6
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tanstack/react-router",
|
|
3
3
|
"author": "Tanner Linsley",
|
|
4
|
-
"version": "0.0.1-beta.
|
|
4
|
+
"version": "0.0.1-beta.209",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": "tanstack/router",
|
|
7
7
|
"homepage": "https://tanstack.com/router",
|
|
@@ -42,7 +42,7 @@
|
|
|
42
42
|
"@babel/runtime": "^7.16.7",
|
|
43
43
|
"tiny-invariant": "^1.3.1",
|
|
44
44
|
"tiny-warning": "^1.0.3",
|
|
45
|
-
"@tanstack/history": "0.0.1-beta.
|
|
45
|
+
"@tanstack/history": "0.0.1-beta.209"
|
|
46
46
|
},
|
|
47
47
|
"scripts": {
|
|
48
48
|
"build": "rollup --config rollup.config.js"
|
package/src/RouteMatch.ts
CHANGED
|
@@ -18,7 +18,7 @@ export interface RouteMatch<
|
|
|
18
18
|
updatedAt: number
|
|
19
19
|
loadPromise?: Promise<void>
|
|
20
20
|
__resolveLoadPromise?: () => void
|
|
21
|
-
|
|
21
|
+
context: RouteById<TRouteTree, TRouteId>['types']['allContext']
|
|
22
22
|
routeSearch: RouteById<TRouteTree, TRouteId>['types']['searchSchema']
|
|
23
23
|
search: FullSearchSchema<TRouteTree> &
|
|
24
24
|
RouteById<TRouteTree, TRouteId>['types']['fullSearchSchema']
|
package/src/RouterProvider.tsx
CHANGED
|
@@ -159,13 +159,13 @@ export function RouterProvider<
|
|
|
159
159
|
const options = {
|
|
160
160
|
...router.options,
|
|
161
161
|
...rest,
|
|
162
|
-
|
|
163
|
-
...router.options.
|
|
164
|
-
...rest?.
|
|
162
|
+
context: {
|
|
163
|
+
...router.options.context,
|
|
164
|
+
...rest?.context,
|
|
165
165
|
},
|
|
166
166
|
} as PickAsRequired<
|
|
167
167
|
RouterOptions<TRouteTree, TDehydrated>,
|
|
168
|
-
'stringifySearch' | 'parseSearch' | '
|
|
168
|
+
'stringifySearch' | 'parseSearch' | 'context'
|
|
169
169
|
>
|
|
170
170
|
|
|
171
171
|
const history = React.useState(
|
|
@@ -451,7 +451,7 @@ export function RouterProvider<
|
|
|
451
451
|
paramsError: parseErrors[index],
|
|
452
452
|
searchError: undefined,
|
|
453
453
|
loadPromise: Promise.resolve(),
|
|
454
|
-
|
|
454
|
+
context: undefined!,
|
|
455
455
|
abortController: new AbortController(),
|
|
456
456
|
fetchedAt: 0,
|
|
457
457
|
}
|
|
@@ -459,9 +459,9 @@ export function RouterProvider<
|
|
|
459
459
|
return routeMatch
|
|
460
460
|
})
|
|
461
461
|
|
|
462
|
-
// Take each match and resolve its search params and
|
|
462
|
+
// Take each match and resolve its search params and context
|
|
463
463
|
// This has to happen after the matches are created or found
|
|
464
|
-
// so that we can use the parent match's search params and
|
|
464
|
+
// so that we can use the parent match's search params and context
|
|
465
465
|
matches.forEach((match, i): any => {
|
|
466
466
|
const parentMatch = matches[i - 1]
|
|
467
467
|
const route = looseRoutesById[match.routeId]!
|
|
@@ -848,26 +848,27 @@ export function RouterProvider<
|
|
|
848
848
|
handleError(match.searchError, 'VALIDATE_SEARCH')
|
|
849
849
|
}
|
|
850
850
|
|
|
851
|
-
const
|
|
851
|
+
const parentContext = parentMatch?.context ?? options.context ?? {}
|
|
852
852
|
|
|
853
|
-
const
|
|
853
|
+
const beforeLoadContext =
|
|
854
854
|
(await route.options.beforeLoad?.({
|
|
855
855
|
search: match.search,
|
|
856
856
|
abortController: match.abortController,
|
|
857
857
|
params: match.params,
|
|
858
858
|
preload: !!preload,
|
|
859
|
-
|
|
859
|
+
context: parentContext,
|
|
860
860
|
location: state.location, // TODO: This might need to be latestLocationRef.current...?
|
|
861
|
+
navigate: (opts) => navigate({ ...opts, from: match.pathname }),
|
|
861
862
|
})) ?? ({} as any)
|
|
862
863
|
|
|
863
|
-
const
|
|
864
|
-
...
|
|
865
|
-
...
|
|
864
|
+
const context = {
|
|
865
|
+
...parentContext,
|
|
866
|
+
...beforeLoadContext,
|
|
866
867
|
}
|
|
867
868
|
|
|
868
869
|
matches[index] = match = {
|
|
869
870
|
...match,
|
|
870
|
-
|
|
871
|
+
context: replaceEqualDeep(match.context, context),
|
|
871
872
|
}
|
|
872
873
|
} catch (err) {
|
|
873
874
|
handleError(err, 'BEFORE_LOAD')
|
|
@@ -924,7 +925,10 @@ export function RouterProvider<
|
|
|
924
925
|
preload: !!preload,
|
|
925
926
|
parentMatchPromise,
|
|
926
927
|
abortController: match.abortController,
|
|
927
|
-
|
|
928
|
+
context: match.context,
|
|
929
|
+
location: state.location,
|
|
930
|
+
navigate: (opts) =>
|
|
931
|
+
navigate({ ...opts, from: match.pathname }),
|
|
928
932
|
})
|
|
929
933
|
|
|
930
934
|
await Promise.all([componentsPromise, loaderPromise])
|
package/src/fileRoute.ts
CHANGED
|
@@ -4,7 +4,7 @@ import {
|
|
|
4
4
|
ResolveFullPath,
|
|
5
5
|
ResolveFullSearchSchema,
|
|
6
6
|
MergeFromFromParent,
|
|
7
|
-
|
|
7
|
+
RouteContext,
|
|
8
8
|
AnyContext,
|
|
9
9
|
RouteOptions,
|
|
10
10
|
UpdatableRouteOptions,
|
|
@@ -97,11 +97,13 @@ export class FileRoute<
|
|
|
97
97
|
TParentRoute['types']['allParams'],
|
|
98
98
|
TParams
|
|
99
99
|
>,
|
|
100
|
-
|
|
100
|
+
TRouteContext extends RouteConstraints['TRouteContext'] = RouteContext,
|
|
101
101
|
TContext extends Expand<
|
|
102
|
-
Assign<IsAny<TParentRoute['types']['
|
|
103
|
-
> = Expand<
|
|
104
|
-
|
|
102
|
+
Assign<IsAny<TParentRoute['types']['allContext'], {}>, TRouteContext>
|
|
103
|
+
> = Expand<
|
|
104
|
+
Assign<IsAny<TParentRoute['types']['allContext'], {}>, TRouteContext>
|
|
105
|
+
>,
|
|
106
|
+
TRouterContext extends RouteConstraints['TRouterContext'] = AnyContext,
|
|
105
107
|
TChildren extends RouteConstraints['TChildren'] = unknown,
|
|
106
108
|
TRouteTree extends RouteConstraints['TRouteTree'] = AnyRoute,
|
|
107
109
|
>(
|
|
@@ -114,7 +116,7 @@ export class FileRoute<
|
|
|
114
116
|
TFullSearchSchema,
|
|
115
117
|
TParams,
|
|
116
118
|
TAllParams,
|
|
117
|
-
|
|
119
|
+
TRouteContext,
|
|
118
120
|
TContext
|
|
119
121
|
>,
|
|
120
122
|
'getParentRoute' | 'path' | 'id'
|
|
@@ -130,9 +132,9 @@ export class FileRoute<
|
|
|
130
132
|
TFullSearchSchema,
|
|
131
133
|
TParams,
|
|
132
134
|
TAllParams,
|
|
133
|
-
|
|
135
|
+
TRouteContext,
|
|
134
136
|
TContext,
|
|
135
|
-
|
|
137
|
+
TRouterContext,
|
|
136
138
|
TChildren,
|
|
137
139
|
TRouteTree
|
|
138
140
|
> => {
|
package/src/react.tsx
CHANGED
|
@@ -40,7 +40,7 @@ export type RouteProps<
|
|
|
40
40
|
useMatch: <TSelected = TAllContext>(opts?: {
|
|
41
41
|
select?: (search: TAllContext) => TSelected
|
|
42
42
|
}) => TSelected
|
|
43
|
-
|
|
43
|
+
useRouteContext: <TSelected = TAllContext>(opts?: {
|
|
44
44
|
select?: (search: TAllContext) => TSelected
|
|
45
45
|
}) => TSelected
|
|
46
46
|
useSearch: <TSelected = TFullSearchSchema>(opts?: {
|
|
@@ -398,7 +398,7 @@ export type RouterProps<
|
|
|
398
398
|
TDehydrated extends Record<string, any> = Record<string, any>,
|
|
399
399
|
> = Omit<RouterOptions<TRouteTree, TDehydrated>, 'context'> & {
|
|
400
400
|
router: Router<TRouteTree>
|
|
401
|
-
context?: Partial<RouterOptions<TRouteTree, TDehydrated>['
|
|
401
|
+
context?: Partial<RouterOptions<TRouteTree, TDehydrated>['context']>
|
|
402
402
|
}
|
|
403
403
|
|
|
404
404
|
export function useRouter<
|
|
@@ -515,21 +515,23 @@ export type RouteFromIdOrRoute<
|
|
|
515
515
|
? RouteIds<TRouteTree>
|
|
516
516
|
: never
|
|
517
517
|
|
|
518
|
-
export function
|
|
518
|
+
export function useRouteContext<
|
|
519
519
|
TRouteTree extends AnyRoute = RegisteredRouter['routeTree'],
|
|
520
520
|
TFrom extends RouteIds<TRouteTree> = RouteIds<TRouteTree>,
|
|
521
521
|
TStrict extends boolean = true,
|
|
522
|
-
|
|
523
|
-
TSelected =
|
|
522
|
+
TRouteContext = RouteById<TRouteTree, TFrom>['types']['allContext'],
|
|
523
|
+
TSelected = TRouteContext,
|
|
524
524
|
>(
|
|
525
525
|
opts: StrictOrFrom<TFrom> & {
|
|
526
|
-
select?: (search:
|
|
526
|
+
select?: (search: TRouteContext) => TSelected
|
|
527
527
|
},
|
|
528
528
|
): TStrict extends true ? TSelected : TSelected | undefined {
|
|
529
529
|
return useMatch({
|
|
530
530
|
...(opts as any),
|
|
531
531
|
select: (match: RouteMatch) =>
|
|
532
|
-
opts?.select
|
|
532
|
+
opts?.select
|
|
533
|
+
? opts.select(match.context as TRouteContext)
|
|
534
|
+
: match.context,
|
|
533
535
|
})
|
|
534
536
|
}
|
|
535
537
|
|
|
@@ -598,6 +600,20 @@ export function useNavigate<
|
|
|
598
600
|
)
|
|
599
601
|
}
|
|
600
602
|
|
|
603
|
+
export function typedNavigate<
|
|
604
|
+
TRouteTree extends AnyRoute = RegisteredRouter['routeTree'],
|
|
605
|
+
TDefaultFrom extends RoutePaths<TRouteTree> = '/',
|
|
606
|
+
>(navigate: (opts: NavigateOptions<any>) => Promise<void>) {
|
|
607
|
+
return navigate as <
|
|
608
|
+
TFrom extends RoutePaths<TRouteTree> = TDefaultFrom,
|
|
609
|
+
TTo extends string = '',
|
|
610
|
+
TMaskFrom extends RoutePaths<TRouteTree> = '/',
|
|
611
|
+
TMaskTo extends string = '',
|
|
612
|
+
>(
|
|
613
|
+
opts?: NavigateOptions<TRouteTree, TFrom, TTo, TMaskFrom, TMaskTo>,
|
|
614
|
+
) => Promise<void>
|
|
615
|
+
}
|
|
616
|
+
|
|
601
617
|
export function useMatchRoute<
|
|
602
618
|
TRouteTree extends AnyRoute = RegisteredRouter['routeTree'],
|
|
603
619
|
>() {
|
|
@@ -652,7 +668,7 @@ export function Matches() {
|
|
|
652
668
|
return React.createElement(ErrorComponent, {
|
|
653
669
|
...props,
|
|
654
670
|
useMatch: route.useMatch,
|
|
655
|
-
|
|
671
|
+
useRouteContext: route.useRouteContext,
|
|
656
672
|
useSearch: route.useSearch,
|
|
657
673
|
useParams: route.useParams,
|
|
658
674
|
})
|
|
@@ -741,7 +757,7 @@ function Match({ matches }: { matches: RouteMatch[] }) {
|
|
|
741
757
|
return React.createElement(routeErrorComponent, {
|
|
742
758
|
...props,
|
|
743
759
|
useMatch: route.useMatch,
|
|
744
|
-
|
|
760
|
+
useRouteContext: route.useRouteContext,
|
|
745
761
|
useSearch: route.useSearch,
|
|
746
762
|
useParams: route.useParams,
|
|
747
763
|
})
|
|
@@ -754,7 +770,7 @@ function Match({ matches }: { matches: RouteMatch[] }) {
|
|
|
754
770
|
<ResolvedSuspenseBoundary
|
|
755
771
|
fallback={React.createElement(PendingComponent, {
|
|
756
772
|
useMatch: route.useMatch,
|
|
757
|
-
|
|
773
|
+
useRouteContext: route.useRouteContext,
|
|
758
774
|
useSearch: route.useSearch,
|
|
759
775
|
useParams: route.useParams,
|
|
760
776
|
})}
|
|
@@ -791,7 +807,7 @@ function MatchInner({ match }: { match: RouteMatch }): any {
|
|
|
791
807
|
if (comp) {
|
|
792
808
|
return React.createElement(comp, {
|
|
793
809
|
useMatch: route.useMatch,
|
|
794
|
-
|
|
810
|
+
useRouteContext: route.useRouteContext as any,
|
|
795
811
|
useSearch: route.useSearch,
|
|
796
812
|
useParams: route.useParams as any,
|
|
797
813
|
} as any)
|