@tanstack/react-router 1.44.4 → 1.45.0

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tanstack/react-router",
3
- "version": "1.44.4",
3
+ "version": "1.45.0",
4
4
  "description": "Modern and scalable routing for React applications",
5
5
  "author": "Tanner Linsley",
6
6
  "license": "MIT",
package/src/route.ts CHANGED
@@ -765,7 +765,7 @@ export class Route<
765
765
  this.to = fullPath as TrimPathRight<TFullPath>
766
766
  }
767
767
 
768
- addChildren = <
768
+ addChildren<
769
769
  const TNewChildren extends
770
770
  | Record<string, AnyRoute>
771
771
  | ReadonlyArray<AnyRoute>,
@@ -791,7 +791,7 @@ export class Route<
791
791
  TLoaderDataReturn,
792
792
  TLoaderData,
793
793
  TNewChildren
794
- > => {
794
+ > {
795
795
  this.children = (
796
796
  Array.isArray(children) ? children : Object.values(children)
797
797
  ) as any
@@ -1061,6 +1061,7 @@ export class RootRoute<
1061
1061
  TLoaderDeps extends Record<string, any> = {},
1062
1062
  TLoaderDataReturn = {},
1063
1063
  in out TLoaderData = ResolveLoaderData<TLoaderDataReturn>,
1064
+ TChildren = unknown,
1064
1065
  > extends Route<
1065
1066
  any, // TParentRoute
1066
1067
  '/', // TPath
@@ -1080,7 +1081,7 @@ export class RootRoute<
1080
1081
  TLoaderDeps,
1081
1082
  TLoaderDataReturn,
1082
1083
  TLoaderData,
1083
- any // TChildren
1084
+ TChildren // TChildren
1084
1085
  > {
1085
1086
  /**
1086
1087
  * @deprecated `RootRoute` is now an internal implementation detail. Use `createRootRoute()` instead.
@@ -1099,6 +1100,27 @@ export class RootRoute<
1099
1100
  ) {
1100
1101
  super(options as any)
1101
1102
  }
1103
+
1104
+ addChildren<
1105
+ const TNewChildren extends
1106
+ | Record<string, AnyRoute>
1107
+ | ReadonlyArray<AnyRoute>,
1108
+ >(
1109
+ children: TNewChildren,
1110
+ ): RootRoute<
1111
+ TSearchSchemaInput,
1112
+ TSearchSchema,
1113
+ TSearchSchemaUsed,
1114
+ TRouteContextReturn,
1115
+ TRouteContext,
1116
+ TRouterContext,
1117
+ TLoaderDeps,
1118
+ TLoaderDataReturn,
1119
+ TLoaderData,
1120
+ TNewChildren
1121
+ > {
1122
+ return super.addChildren(children)
1123
+ }
1102
1124
  }
1103
1125
 
1104
1126
  export function createRootRoute<
package/src/router.ts CHANGED
@@ -104,6 +104,7 @@ export interface Register {
104
104
  }
105
105
 
106
106
  export type AnyRouter = Router<any, any, any, any>
107
+
107
108
  export type AnyRouterWithContext<TContext> = Router<
108
109
  AnyRouteWithContext<TContext>,
109
110
  any,
@@ -132,6 +133,7 @@ export type InferRouterContext<TRouteTree extends AnyRoute> =
132
133
  infer TRouterContext extends AnyContext,
133
134
  any,
134
135
  any,
136
+ any,
135
137
  any
136
138
  >
137
139
  ? TRouterContext
package/src/useMatch.tsx CHANGED
@@ -13,8 +13,10 @@ export type UseMatchOptions<
13
13
  TStrict extends boolean,
14
14
  TRouteMatch,
15
15
  TSelected,
16
+ TThrow extends boolean,
16
17
  > = StrictOrFrom<TFrom, TStrict> & {
17
18
  select?: (match: TRouteMatch) => TSelected
19
+ shouldThrow?: TThrow
18
20
  }
19
21
 
20
22
  export function useMatch<
@@ -23,7 +25,10 @@ export function useMatch<
23
25
  TStrict extends boolean = true,
24
26
  TRouteMatch = MakeRouteMatch<TRouteTree, TFrom, TStrict>,
25
27
  TSelected = TRouteMatch,
26
- >(opts: UseMatchOptions<TFrom, TStrict, TRouteMatch, TSelected>): TSelected {
28
+ TThrow extends boolean = true,
29
+ >(
30
+ opts: UseMatchOptions<TFrom, TStrict, TRouteMatch, TSelected, TThrow>,
31
+ ): TThrow extends true ? TSelected : TSelected | undefined {
27
32
  const nearestMatchId = React.useContext(matchContext)
28
33
 
29
34
  const matchSelection = useRouterState({
@@ -31,12 +36,15 @@ export function useMatch<
31
36
  const match = state.matches.find((d) =>
32
37
  opts.from ? opts.from === d.routeId : d.id === nearestMatchId,
33
38
  )
34
-
35
39
  invariant(
36
- match,
40
+ !((opts.shouldThrow ?? true) && !match),
37
41
  `Could not find ${opts.from ? `an active match from "${opts.from}"` : 'a nearest match!'}`,
38
42
  )
39
43
 
44
+ if (match === undefined) {
45
+ return undefined
46
+ }
47
+
40
48
  return opts.select ? opts.select(match as any) : match
41
49
  },
42
50
  })