@tanstack/react-router 0.0.1-beta.233 → 0.0.1-beta.235

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.
@@ -45,13 +45,7 @@ export type MakeMatchRouteOptions<TRouteTree extends AnyRoute = RegisteredRouter
45
45
  children?: ((params?: RouteByPath<TRouteTree, ResolveRelativePath<TFrom, NoInfer<TTo>>>['types']['allParams']) => ReactNode) | React.ReactNode;
46
46
  };
47
47
  export declare function MatchRoute<TRouteTree extends AnyRoute = RegisteredRouter['routeTree'], TFrom extends RoutePaths<TRouteTree> = '/', TTo extends string = '', TMaskFrom extends RoutePaths<TRouteTree> = '/', TMaskTo extends string = ''>(props: MakeMatchRouteOptions<TRouteTree, TFrom, TTo, TMaskFrom, TMaskTo>): any;
48
- export declare function useMatch<TRouteTree extends AnyRoute = RegisteredRouter['routeTree'], TFrom extends RouteIds<TRouteTree> = RouteIds<TRouteTree>, TStrict extends boolean = true, TRouteMatchState = RouteMatch<TRouteTree, TFrom>, TSelected = TRouteMatchState>(opts: StrictOrFrom<TFrom> & {
49
- select?: (match: TRouteMatchState) => TSelected;
50
- }): TStrict extends true ? TSelected : TSelected | undefined;
48
+ export declare function useMatch<TRouteTree extends AnyRoute = RegisteredRouter['routeTree'], TFrom extends RouteIds<TRouteTree> = RouteIds<TRouteTree>, TStrict extends boolean = true, TRouteMatchState = RouteMatch<TRouteTree, TFrom>>(opts: StrictOrFrom<TFrom>): TStrict extends true ? TRouteMatchState : TRouteMatchState | undefined;
51
49
  export declare const matchesContext: React.Context<RouteMatch<AnyRoute, any>[]>;
52
- export declare function useMatches<T = RouteMatch[]>(opts?: {
53
- select?: (matches: RouteMatch[]) => T;
54
- }): T;
55
- export declare function useLoaderData<TRouteTree extends AnyRoute = RegisteredRouter['routeTree'], TFrom extends RouteIds<TRouteTree> = RouteIds<TRouteTree>, TStrict extends boolean = true, TRouteMatch extends RouteMatch<TRouteTree, TFrom> = RouteMatch<TRouteTree, TFrom>, TSelected = TRouteMatch['loaderData']>(opts: StrictOrFrom<TFrom> & {
56
- select?: (match: TRouteMatch) => TSelected;
57
- }): TStrict extends true ? TSelected : TSelected | undefined;
50
+ export declare function useMatches(): RouteMatch[];
51
+ export declare function useLoaderData<TRouteTree extends AnyRoute = RegisteredRouter['routeTree'], TFrom extends RouteIds<TRouteTree> = RouteIds<TRouteTree>, TStrict extends boolean = true, TRouteMatch extends RouteMatch<TRouteTree, TFrom> = RouteMatch<TRouteTree, TFrom>>(opts: StrictOrFrom<TFrom>): TStrict extends true ? TRouteMatch['loaderData'] : TRouteMatch['loaderData'] | undefined;
@@ -26,9 +26,6 @@ export type InjectedHtmlEntry = string | (() => Promise<string> | string);
26
26
  export declare const routerContext: React.Context<Router<any, Record<string, any>>>;
27
27
  export declare function RouterProvider<TRouteTree extends AnyRoute = RegisteredRouter['routeTree'], TDehydrated extends Record<string, any> = Record<string, any>>({ router, ...rest }: RouterProps<TRouteTree, TDehydrated>): React.JSX.Element;
28
28
  export declare function getRouteMatch<TRouteTree extends AnyRoute>(state: RouterState<TRouteTree>, id: string): undefined | RouteMatch<TRouteTree>;
29
- export declare function useRouterState<TSelected = RouterState<RegisteredRouter['routeTree']>>(opts?: {
30
- select: (state: RouterState<RegisteredRouter['routeTree']>) => TSelected;
31
- }): TSelected;
32
29
  export type RouterProps<TRouteTree extends AnyRoute = RegisteredRouter['routeTree'], TDehydrated extends Record<string, any> = Record<string, any>> = Omit<RouterOptions<TRouteTree, TDehydrated>, 'context'> & {
33
30
  router: Router<TRouteTree>;
34
31
  context?: Partial<RouterOptions<TRouteTree, TDehydrated>['context']>;
@@ -180,21 +180,11 @@ export declare class Route<TParentRoute extends RouteConstraints['TParentRoute']
180
180
  addChildren: <TNewChildren extends AnyRoute[]>(children: TNewChildren) => Route<TParentRoute, TPath, TFullPath, TCustomId, TId, TSearchSchema, TFullSearchSchema, TParams, TAllParams, TRouteContext, TAllContext, TRouterContext, TNewChildren, TRouteTree, AnyRoute>;
181
181
  update: (options: UpdatableRouteOptions<TFullSearchSchema, TAllParams, Expand<Assign<IsAny<TParentRoute['types']['allContext'], {}>, TRouteContext>>, TLoaderData>) => this;
182
182
  static __onInit: (route: any) => void;
183
- useMatch: <TSelected = TAllContext>(opts?: {
184
- select?: ((search: TAllContext) => TSelected) | undefined;
185
- } | undefined) => TSelected;
186
- useRouteContext: <TSelected = TAllContext>(opts?: {
187
- select?: ((search: TAllContext) => TSelected) | undefined;
188
- } | undefined) => TSelected;
189
- useSearch: <TSelected = TFullSearchSchema>(opts?: {
190
- select?: ((search: TFullSearchSchema) => TSelected) | undefined;
191
- } | undefined) => TSelected;
192
- useParams: <TSelected = TAllParams>(opts?: {
193
- select?: ((search: TAllParams) => TSelected) | undefined;
194
- } | undefined) => TSelected;
195
- useLoaderData: <TSelected = TLoaderData>(opts?: {
196
- select?: ((search: TLoaderData) => TSelected) | undefined;
197
- } | undefined) => TSelected;
183
+ useMatch: () => TAllContext;
184
+ useRouteContext: () => TAllContext;
185
+ useSearch: () => TFullSearchSchema;
186
+ useParams: () => TAllParams;
187
+ useLoaderData: () => TLoaderData;
198
188
  }
199
189
  export type AnyRootRoute = RootRoute<any, any, any>;
200
190
  export declare function rootRouteWithContext<TRouterContext extends {}>(): <TSearchSchema extends Record<string, any> = {}, TRouteContext extends RouteContext = RouteContext, TLoaderData extends unknown = unknown>(options?: Omit<RouteOptions<AnyRoute, "__root__", "", TSearchSchema, TSearchSchema, {}, {}, TRouteContext, Assign<TRouterContext, TRouteContext>, TLoaderData>, "path" | "id" | "getParentRoute" | "stringifyParams" | "parseParams" | "caseSensitive"> | undefined) => RootRoute<TSearchSchema, TRouteContext, TRouterContext, unknown>;
@@ -242,21 +232,11 @@ export declare function createRouteMask<TRouteTree extends AnyRoute, TFrom exten
242
232
  routeTree: TRouteTree;
243
233
  } & ToSubOptions<TRouteTree, TFrom, TTo>): RouteMask<TRouteTree>;
244
234
  export type RouteProps<TFullSearchSchema extends Record<string, any> = AnySearchSchema, TAllParams extends AnyPathParams = AnyPathParams, TAllContext extends Record<string, any> = AnyContext, TLoaderData extends any = unknown> = {
245
- useMatch: <TSelected = TAllContext>(opts?: {
246
- select?: (search: TAllContext) => TSelected;
247
- }) => TSelected;
248
- useRouteContext: <TSelected = TAllContext>(opts?: {
249
- select?: (search: TAllContext) => TSelected;
250
- }) => TSelected;
251
- useSearch: <TSelected = TFullSearchSchema>(opts?: {
252
- select?: (search: TFullSearchSchema) => TSelected;
253
- }) => TSelected;
254
- useParams: <TSelected = TAllParams>(opts?: {
255
- select?: (search: TAllParams) => TSelected;
256
- }) => TSelected;
257
- useLoaderData: <TSelected = TLoaderData>(opts?: {
258
- select?: (search: TLoaderData) => TSelected;
259
- }) => TSelected;
235
+ useMatch: () => TAllContext;
236
+ useRouteContext: () => TAllContext;
237
+ useSearch: () => TFullSearchSchema;
238
+ useParams: () => TAllParams;
239
+ useLoaderData: () => TLoaderData;
260
240
  };
261
241
  export type ErrorRouteProps<TFullSearchSchema extends Record<string, any> = AnySearchSchema, TAllParams extends AnyPathParams = AnyPathParams, TAllContext extends Record<string, any> = AnyContext> = {
262
242
  error: unknown;
@@ -2,6 +2,4 @@ import { AnyRoute } from './route';
2
2
  import { RouteIds, RouteById, AllParams } from './routeInfo';
3
3
  import { RegisteredRouter } from './router';
4
4
  import { StrictOrFrom } from './utils';
5
- export declare function useParams<TRouteTree extends AnyRoute = RegisteredRouter['routeTree'], TFrom extends RouteIds<TRouteTree> = RouteIds<TRouteTree>, TDefaultSelected = AllParams<TRouteTree> & RouteById<TRouteTree, TFrom>['types']['allParams'], TSelected = TDefaultSelected>(opts: StrictOrFrom<TFrom> & {
6
- select?: (search: TDefaultSelected) => TSelected;
7
- }): TSelected;
5
+ export declare function useParams<TRouteTree extends AnyRoute = RegisteredRouter['routeTree'], TFrom extends RouteIds<TRouteTree> = RouteIds<TRouteTree>>(_opts: StrictOrFrom<TFrom>): AllParams<TRouteTree> & RouteById<TRouteTree, TFrom>['types']['allParams'];
@@ -2,6 +2,4 @@ import { AnyRoute } from './route';
2
2
  import { RouteIds, RouteById } from './routeInfo';
3
3
  import { RegisteredRouter } from './router';
4
4
  import { StrictOrFrom } from './utils';
5
- export declare function useSearch<TRouteTree extends AnyRoute = RegisteredRouter['routeTree'], TFrom extends RouteIds<TRouteTree> = RouteIds<TRouteTree>, TStrict extends boolean = true, TSearch = RouteById<TRouteTree, TFrom>['types']['fullSearchSchema'], TSelected = TSearch>(opts: StrictOrFrom<TFrom> & {
6
- select?: (search: TSearch) => TSelected;
7
- }): TStrict extends true ? TSelected : TSelected | undefined;
5
+ export declare function useSearch<TRouteTree extends AnyRoute = RegisteredRouter['routeTree'], TFrom extends RouteIds<TRouteTree> = RouteIds<TRouteTree>, TStrict extends boolean = true, TSearch = RouteById<TRouteTree, TFrom>['types']['fullSearchSchema']>(opts: StrictOrFrom<TFrom>): TStrict extends true ? TSearch : TSearch | undefined;
@@ -59,8 +59,6 @@ export type StrictOrFrom<TFrom> = {
59
59
  strict: false;
60
60
  };
61
61
  export type RouteFromIdOrRoute<T, TRouteTree extends AnyRoute = RegisteredRouter['routeTree']> = T extends ParseRoute<TRouteTree> ? T : T extends RouteIds<TRouteTree> ? RoutesById<TRouteTree>[T] : T extends string ? RouteIds<TRouteTree> : never;
62
- export declare function useRouteContext<TRouteTree extends AnyRoute = RegisteredRouter['routeTree'], TFrom extends RouteIds<TRouteTree> = RouteIds<TRouteTree>, TStrict extends boolean = true, TRouteContext = RouteById<TRouteTree, TFrom>['types']['allContext'], TSelected = TRouteContext>(opts: StrictOrFrom<TFrom> & {
63
- select?: (search: TRouteContext) => TSelected;
64
- }): TStrict extends true ? TSelected : TSelected | undefined;
62
+ export declare function useRouteContext<TRouteTree extends AnyRoute = RegisteredRouter['routeTree'], TFrom extends RouteIds<TRouteTree> = RouteIds<TRouteTree>, TStrict extends boolean = true, TRouteContext = RouteById<TRouteTree, TFrom>['types']['allContext'], TSelected = TRouteContext>(opts: StrictOrFrom<TFrom>): TStrict extends true ? TSelected : TSelected | undefined;
65
63
  export declare const useLayoutEffect: typeof React.useLayoutEffect;
66
64
  export declare function escapeJSON(jsonString: string): string;
@@ -608,10 +608,7 @@
608
608
  return true;
609
609
  }
610
610
  function useRouteContext(opts) {
611
- return useMatch({
612
- ...opts,
613
- select: match => opts?.select ? opts.select(match.context) : match.context
614
- });
611
+ return useMatch(opts).context;
615
612
  }
616
613
  const useLayoutEffect$1 = typeof window !== 'undefined' ? React__namespace.useLayoutEffect : React__namespace.useEffect;
617
614
  function escapeJSON(jsonString) {
@@ -804,22 +801,12 @@
804
801
  return isMatch ? params : undefined;
805
802
  }
806
803
 
807
- function useParams(opts) {
808
- return useRouterState({
809
- select: state => {
810
- const params = last(state.matches)?.params;
811
- return opts?.select ? opts.select(params) : params;
812
- }
813
- });
804
+ function useParams(_opts) {
805
+ return last(useRouter().state.matches)?.params;
814
806
  }
815
807
 
816
808
  function useSearch(opts) {
817
- return useMatch({
818
- ...opts,
819
- select: match => {
820
- return opts?.select ? opts.select(match.search) : match.search;
821
- }
822
- });
809
+ return useMatch(opts).search;
823
810
  }
824
811
 
825
812
  const rootRouteId = '__root__';
@@ -884,34 +871,28 @@
884
871
  // This is a dummy static method that should get
885
872
  // replaced by a framework specific implementation if necessary
886
873
  };
887
- useMatch = opts => {
874
+ useMatch = () => {
888
875
  return useMatch({
889
- ...opts,
890
876
  from: this.id
891
877
  });
892
878
  };
893
- useRouteContext = opts => {
879
+ useRouteContext = () => {
894
880
  return useMatch({
895
- ...opts,
896
- from: this.id,
897
- select: d => opts?.select ? opts.select(d.context) : d.context
898
- });
881
+ from: this.id
882
+ }).context;
899
883
  };
900
- useSearch = opts => {
884
+ useSearch = () => {
901
885
  return useSearch({
902
- ...opts,
903
886
  from: this.id
904
887
  });
905
888
  };
906
- useParams = opts => {
889
+ useParams = () => {
907
890
  return useParams({
908
- ...opts,
909
891
  from: this.id
910
892
  });
911
893
  };
912
- useLoaderData = opts => {
894
+ useLoaderData = () => {
913
895
  return useLoaderData({
914
- ...opts,
915
896
  from: this.id
916
897
  });
917
898
  };
@@ -933,15 +914,12 @@
933
914
  //
934
915
 
935
916
  function Matches() {
936
- const {
937
- routesById,
938
- state
939
- } = useRouter();
917
+ const router = useRouter();
940
918
  const {
941
919
  matches
942
- } = state;
943
- const locationKey = useRouterState().location.state.key;
944
- const route = routesById[rootRouteId];
920
+ } = router.state;
921
+ const locationKey = router.state.location.state.key;
922
+ const route = router.routesById[rootRouteId];
945
923
  const errorComponent = React__namespace.useCallback(props => {
946
924
  return /*#__PURE__*/React__namespace.createElement(ErrorComponent, {
947
925
  ...props,
@@ -1081,42 +1059,31 @@
1081
1059
  return !!params ? props.children : null;
1082
1060
  }
1083
1061
  function useMatch(opts) {
1062
+ const router = useRouter();
1084
1063
  const nearestMatch = React__namespace.useContext(matchesContext)[0];
1085
1064
  const nearestMatchRouteId = nearestMatch?.routeId;
1086
- const matchRouteId = useRouterState({
1087
- select: state => {
1088
- const match = opts?.from ? state.matches.find(d => d.routeId === opts?.from) : state.matches.find(d => d.id === nearestMatch.id);
1089
- return match.routeId;
1090
- }
1091
- });
1065
+ const matchRouteId = (() => {
1066
+ const match = opts?.from ? router.state.matches.find(d => d.routeId === opts?.from) : router.state.matches.find(d => d.id === nearestMatch.id);
1067
+ return match.routeId;
1068
+ })();
1092
1069
  if (opts?.strict ?? true) {
1093
1070
  invariant(nearestMatchRouteId == matchRouteId, `useMatch("${matchRouteId}") is being called in a component that is meant to render the '${nearestMatchRouteId}' route. Did you mean to 'useMatch("${matchRouteId}", { strict: false })' or 'useRoute("${matchRouteId}")' instead?`);
1094
1071
  }
1095
- const matchSelection = useRouterState({
1096
- select: state => {
1097
- const match = opts?.from ? state.matches.find(d => d.routeId === opts?.from) : state.matches.find(d => d.id === nearestMatch.id);
1098
- invariant(match, `Could not find ${opts?.from ? `an active match from "${opts.from}"` : 'a nearest match!'}`);
1099
- return opts?.select ? opts.select(match) : match;
1100
- }
1101
- });
1072
+ const matchSelection = (() => {
1073
+ const match = opts?.from ? router.state.matches.find(d => d.routeId === opts?.from) : router.state.matches.find(d => d.id === nearestMatch.id);
1074
+ invariant(match, `Could not find ${opts?.from ? `an active match from "${opts.from}"` : 'a nearest match!'}`);
1075
+ return match;
1076
+ })();
1102
1077
  return matchSelection;
1103
1078
  }
1104
1079
  const matchesContext = /*#__PURE__*/React__namespace.createContext(null);
1105
- function useMatches(opts) {
1080
+ function useMatches() {
1081
+ const router = useRouter();
1106
1082
  const contextMatches = React__namespace.useContext(matchesContext);
1107
- return useRouterState({
1108
- select: state => {
1109
- const matches = state.matches.slice(state.matches.findIndex(d => d.id === contextMatches[0]?.id));
1110
- return opts?.select ? opts.select(matches) : matches;
1111
- }
1112
- });
1083
+ return router.state.matches.slice(router.state.matches.findIndex(d => d.id === contextMatches[0]?.id));
1113
1084
  }
1114
1085
  function useLoaderData(opts) {
1115
- const match = useMatch({
1116
- ...opts,
1117
- select: undefined
1118
- });
1119
- return typeof opts.select === 'function' ? opts.select(match?.loaderData) : match?.loaderData;
1086
+ return useMatch(opts)?.loaderData;
1120
1087
  }
1121
1088
 
1122
1089
  const routerContext = /*#__PURE__*/React__namespace.createContext(null);
@@ -1160,20 +1127,20 @@
1160
1127
  router.state = state;
1161
1128
  router.startReactTransition = startReactTransition;
1162
1129
  const tryLoad = () => {
1163
- if (state.location !== router.latestLocation) {
1164
- startReactTransition(() => {
1165
- try {
1166
- router.load();
1167
- } catch (err) {
1168
- console.error(err);
1169
- }
1170
- });
1171
- }
1130
+ startReactTransition(() => {
1131
+ try {
1132
+ router.load();
1133
+ } catch (err) {
1134
+ console.error(err);
1135
+ }
1136
+ });
1172
1137
  };
1173
1138
  useLayoutEffect$1(() => {
1174
1139
  const unsub = router.history.subscribe(() => {
1175
1140
  router.latestLocation = router.parseLocation(router.latestLocation);
1176
- tryLoad();
1141
+ if (state.location !== router.latestLocation) {
1142
+ tryLoad();
1143
+ }
1177
1144
  });
1178
1145
  const nextLocation = router.buildLocation({
1179
1146
  search: true,
@@ -1218,13 +1185,6 @@
1218
1185
  function getRouteMatch(state, id) {
1219
1186
  return [...state.pendingMatches, ...state.matches].find(d => d.id === id);
1220
1187
  }
1221
- function useRouterState(opts) {
1222
- const {
1223
- state
1224
- } = useRouter();
1225
- // return useStore(router.__store, opts?.select as any)
1226
- return opts?.select ? opts.select(state) : state;
1227
- }
1228
1188
  function useRouter() {
1229
1189
  const resolvedContext = typeof document !== 'undefined' ? window.__TSR_ROUTER_CONTEXT__ || routerContext : routerContext;
1230
1190
  const value = React__namespace.useContext(resolvedContext);
@@ -2922,7 +2882,6 @@
2922
2882
  exports.useParams = useParams;
2923
2883
  exports.useRouteContext = useRouteContext;
2924
2884
  exports.useRouter = useRouter;
2925
- exports.useRouterState = useRouterState;
2926
2885
  exports.useScrollRestoration = useScrollRestoration;
2927
2886
  exports.useSearch = useSearch;
2928
2887
  exports.useStableCallback = useStableCallback;