@tanstack/react-router 0.0.1-beta.234 → 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.
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.234",
4
+ "version": "0.0.1-beta.235",
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.234"
45
+ "@tanstack/history": "0.0.1-beta.235"
46
46
  },
47
47
  "scripts": {
48
48
  "build": "rollup --config rollup.config.js"
package/src/Matches.tsx CHANGED
@@ -2,7 +2,7 @@ import * as React from 'react'
2
2
  import invariant from 'tiny-invariant'
3
3
  import warning from 'tiny-warning'
4
4
  import { CatchBoundary, ErrorComponent } from './CatchBoundary'
5
- import { useRouter, useRouterState } from './RouterProvider'
5
+ import { useRouter } from './RouterProvider'
6
6
  import { ResolveRelativePath, ToOptions } from './link'
7
7
  import { AnyRoute, ReactNode, rootRouteId } from './route'
8
8
  import {
@@ -47,12 +47,10 @@ export interface RouteMatch<
47
47
  export type AnyRouteMatch = RouteMatch<any>
48
48
 
49
49
  export function Matches() {
50
- const { routesById, state } = useRouter()
51
- const { matches } = state
52
-
53
- const locationKey = useRouterState().location.state.key
54
-
55
- const route = routesById[rootRouteId]!
50
+ const router = useRouter()
51
+ const { matches } = router.state
52
+ const locationKey = router.state.location.state.key
53
+ const route = router.routesById[rootRouteId]!
56
54
 
57
55
  const errorComponent = React.useCallback(
58
56
  (props: any) => {
@@ -296,24 +294,20 @@ export function useMatch<
296
294
  TFrom extends RouteIds<TRouteTree> = RouteIds<TRouteTree>,
297
295
  TStrict extends boolean = true,
298
296
  TRouteMatchState = RouteMatch<TRouteTree, TFrom>,
299
- TSelected = TRouteMatchState,
300
297
  >(
301
- opts: StrictOrFrom<TFrom> & {
302
- select?: (match: TRouteMatchState) => TSelected
303
- },
304
- ): TStrict extends true ? TSelected : TSelected | undefined {
298
+ opts: StrictOrFrom<TFrom>,
299
+ ): TStrict extends true ? TRouteMatchState : TRouteMatchState | undefined {
300
+ const router = useRouter()
305
301
  const nearestMatch = React.useContext(matchesContext)[0]!
306
302
  const nearestMatchRouteId = nearestMatch?.routeId
307
303
 
308
- const matchRouteId = useRouterState({
309
- select: (state) => {
310
- const match = opts?.from
311
- ? state.matches.find((d) => d.routeId === opts?.from)
312
- : state.matches.find((d) => d.id === nearestMatch.id)
304
+ const matchRouteId = (() => {
305
+ const match = opts?.from
306
+ ? router.state.matches.find((d) => d.routeId === opts?.from)
307
+ : router.state.matches.find((d) => d.id === nearestMatch.id)
313
308
 
314
- return match!.routeId
315
- },
316
- })
309
+ return match!.routeId
310
+ })()
317
311
 
318
312
  if (opts?.strict ?? true) {
319
313
  invariant(
@@ -328,43 +322,32 @@ export function useMatch<
328
322
  )
329
323
  }
330
324
 
331
- const matchSelection = useRouterState({
332
- select: (state) => {
333
- const match = opts?.from
334
- ? state.matches.find((d) => d.routeId === opts?.from)
335
- : state.matches.find((d) => d.id === nearestMatch.id)
336
-
337
- invariant(
338
- match,
339
- `Could not find ${
340
- opts?.from
341
- ? `an active match from "${opts.from}"`
342
- : 'a nearest match!'
343
- }`,
344
- )
325
+ const matchSelection = (() => {
326
+ const match = opts?.from
327
+ ? router.state.matches.find((d) => d.routeId === opts?.from)
328
+ : router.state.matches.find((d) => d.id === nearestMatch.id)
345
329
 
346
- return opts?.select ? opts.select(match as any) : match
347
- },
348
- })
330
+ invariant(
331
+ match,
332
+ `Could not find ${
333
+ opts?.from ? `an active match from "${opts.from}"` : 'a nearest match!'
334
+ }`,
335
+ )
336
+
337
+ return match
338
+ })()
349
339
 
350
340
  return matchSelection as any
351
341
  }
352
342
 
353
343
  export const matchesContext = React.createContext<RouteMatch[]>(null!)
354
344
 
355
- export function useMatches<T = RouteMatch[]>(opts?: {
356
- select?: (matches: RouteMatch[]) => T
357
- }): T {
345
+ export function useMatches(): RouteMatch[] {
346
+ const router = useRouter()
358
347
  const contextMatches = React.useContext(matchesContext)
359
-
360
- return useRouterState({
361
- select: (state) => {
362
- const matches = state.matches.slice(
363
- state.matches.findIndex((d) => d.id === contextMatches[0]?.id),
364
- )
365
- return opts?.select ? opts.select(matches) : (matches as T)
366
- },
367
- })
348
+ return router.state.matches.slice(
349
+ router.state.matches.findIndex((d) => d.id === contextMatches[0]?.id),
350
+ )
368
351
  }
369
352
 
370
353
  export function useLoaderData<
@@ -375,15 +358,10 @@ export function useLoaderData<
375
358
  TRouteTree,
376
359
  TFrom
377
360
  >,
378
- TSelected = TRouteMatch['loaderData'],
379
361
  >(
380
- opts: StrictOrFrom<TFrom> & {
381
- select?: (match: TRouteMatch) => TSelected
382
- },
383
- ): TStrict extends true ? TSelected : TSelected | undefined {
384
- const match = useMatch({ ...opts, select: undefined })!
385
-
386
- return typeof opts.select === 'function'
387
- ? opts.select(match?.loaderData)
388
- : match?.loaderData
362
+ opts: StrictOrFrom<TFrom>,
363
+ ): TStrict extends true
364
+ ? TRouteMatch['loaderData']
365
+ : TRouteMatch['loaderData'] | undefined {
366
+ return useMatch(opts)?.loaderData
389
367
  }
@@ -189,16 +189,6 @@ export function getRouteMatch<TRouteTree extends AnyRoute>(
189
189
  return [...state.pendingMatches, ...state.matches].find((d) => d.id === id)
190
190
  }
191
191
 
192
- export function useRouterState<
193
- TSelected = RouterState<RegisteredRouter['routeTree']>,
194
- >(opts?: {
195
- select: (state: RouterState<RegisteredRouter['routeTree']>) => TSelected
196
- }): TSelected {
197
- const { state } = useRouter()
198
- // return useStore(router.__store, opts?.select as any)
199
- return opts?.select ? opts.select(state) : (state as any)
200
- }
201
-
202
192
  export type RouterProps<
203
193
  TRouteTree extends AnyRoute = RegisteredRouter['routeTree'],
204
194
  TDehydrated extends Record<string, any> = Record<string, any>,
package/src/route.ts CHANGED
@@ -590,34 +590,22 @@ export class Route<
590
590
  // replaced by a framework specific implementation if necessary
591
591
  }
592
592
 
593
- useMatch = <TSelected = TAllContext>(opts?: {
594
- select?: (search: TAllContext) => TSelected
595
- }): TSelected => {
596
- return useMatch({ ...opts, from: this.id }) as any
593
+ useMatch = (): TAllContext => {
594
+ return useMatch({ from: this.id }) as any
597
595
  }
598
- useRouteContext = <TSelected = TAllContext>(opts?: {
599
- select?: (search: TAllContext) => TSelected
600
- }): TSelected => {
596
+ useRouteContext = (): TAllContext => {
601
597
  return useMatch({
602
- ...opts,
603
598
  from: this.id,
604
- select: (d: any) => (opts?.select ? opts.select(d.context) : d.context),
605
- } as any)
599
+ } as any).context
606
600
  }
607
- useSearch = <TSelected = TFullSearchSchema>(opts?: {
608
- select?: (search: TFullSearchSchema) => TSelected
609
- }): TSelected => {
610
- return useSearch({ ...opts, from: this.id } as any)
601
+ useSearch = (): TFullSearchSchema => {
602
+ return useSearch({ from: this.id } as any)
611
603
  }
612
- useParams = <TSelected = TAllParams>(opts?: {
613
- select?: (search: TAllParams) => TSelected
614
- }): TSelected => {
615
- return useParams({ ...opts, from: this.id } as any)
604
+ useParams = (): TAllParams => {
605
+ return useParams({ from: this.id } as any)
616
606
  }
617
- useLoaderData = <TSelected = TLoaderData>(opts?: {
618
- select?: (search: TLoaderData) => TSelected
619
- }): TSelected => {
620
- return useLoaderData({ ...opts, from: this.id } as any) as any
607
+ useLoaderData = (): TLoaderData => {
608
+ return useLoaderData({ from: this.id } as any) as any
621
609
  }
622
610
  }
623
611
 
@@ -768,21 +756,11 @@ export type RouteProps<
768
756
  TAllContext extends Record<string, any> = AnyContext,
769
757
  TLoaderData extends any = unknown,
770
758
  > = {
771
- useMatch: <TSelected = TAllContext>(opts?: {
772
- select?: (search: TAllContext) => TSelected
773
- }) => TSelected
774
- useRouteContext: <TSelected = TAllContext>(opts?: {
775
- select?: (search: TAllContext) => TSelected
776
- }) => TSelected
777
- useSearch: <TSelected = TFullSearchSchema>(opts?: {
778
- select?: (search: TFullSearchSchema) => TSelected
779
- }) => TSelected
780
- useParams: <TSelected = TAllParams>(opts?: {
781
- select?: (search: TAllParams) => TSelected
782
- }) => TSelected
783
- useLoaderData: <TSelected = TLoaderData>(opts?: {
784
- select?: (search: TLoaderData) => TSelected
785
- }) => TSelected
759
+ useMatch: () => TAllContext
760
+ useRouteContext: () => TAllContext
761
+ useSearch: () => TFullSearchSchema
762
+ useParams: () => TAllParams
763
+ useLoaderData: () => TLoaderData
786
764
  }
787
765
 
788
766
  export type ErrorRouteProps<
package/src/useParams.tsx CHANGED
@@ -2,24 +2,14 @@ import { AnyRoute } from './route'
2
2
  import { RouteIds, RouteById, AllParams } from './routeInfo'
3
3
  import { RegisteredRouter } from './router'
4
4
  import { last } from './utils'
5
- import { useRouterState } from './RouterProvider'
5
+ import { useRouter } from './RouterProvider'
6
6
  import { StrictOrFrom } from './utils'
7
7
 
8
8
  export function useParams<
9
9
  TRouteTree extends AnyRoute = RegisteredRouter['routeTree'],
10
10
  TFrom extends RouteIds<TRouteTree> = RouteIds<TRouteTree>,
11
- TDefaultSelected = AllParams<TRouteTree> &
12
- RouteById<TRouteTree, TFrom>['types']['allParams'],
13
- TSelected = TDefaultSelected,
14
11
  >(
15
- opts: StrictOrFrom<TFrom> & {
16
- select?: (search: TDefaultSelected) => TSelected
17
- },
18
- ): TSelected {
19
- return useRouterState({
20
- select: (state: any) => {
21
- const params = (last(state.matches) as any)?.params
22
- return opts?.select ? opts.select(params) : params
23
- },
24
- })
12
+ _opts: StrictOrFrom<TFrom>,
13
+ ): AllParams<TRouteTree> & RouteById<TRouteTree, TFrom>['types']['allParams'] {
14
+ return (last(useRouter().state.matches) as any)?.params
25
15
  }
package/src/useSearch.tsx CHANGED
@@ -10,16 +10,8 @@ export function useSearch<
10
10
  TFrom extends RouteIds<TRouteTree> = RouteIds<TRouteTree>,
11
11
  TStrict extends boolean = true,
12
12
  TSearch = RouteById<TRouteTree, TFrom>['types']['fullSearchSchema'],
13
- TSelected = TSearch,
14
13
  >(
15
- opts: StrictOrFrom<TFrom> & {
16
- select?: (search: TSearch) => TSelected
17
- },
18
- ): TStrict extends true ? TSelected : TSelected | undefined {
19
- return useMatch({
20
- ...(opts as any),
21
- select: (match: RouteMatch) => {
22
- return opts?.select ? opts.select(match.search as TSearch) : match.search
23
- },
24
- })
14
+ opts: StrictOrFrom<TFrom>,
15
+ ): TStrict extends true ? TSearch : TSearch | undefined {
16
+ return useMatch(opts).search
25
17
  }
package/src/utils.ts CHANGED
@@ -18,8 +18,8 @@ export type PickExtra<T, K> = {
18
18
  [TKey in keyof K as string extends TKey
19
19
  ? never
20
20
  : TKey extends keyof T
21
- ? never
22
- : TKey]: K[TKey]
21
+ ? never
22
+ : TKey]: K[TKey]
23
23
  }
24
24
 
25
25
  export type PickRequired<T> = {
@@ -105,15 +105,15 @@ export type ValueKeys<O> = Extract<keyof O, PropertyKey>
105
105
  export type DeepAwaited<T> = T extends Promise<infer A>
106
106
  ? DeepAwaited<A>
107
107
  : T extends Record<infer A, Promise<infer B>>
108
- ? { [K in A]: DeepAwaited<B> }
109
- : T
108
+ ? { [K in A]: DeepAwaited<B> }
109
+ : T
110
110
 
111
111
  export type PathParamMask<TRoutePath extends string> =
112
112
  TRoutePath extends `${infer L}/$${infer C}/${infer R}`
113
113
  ? PathParamMask<`${L}/${string}/${R}`>
114
114
  : TRoutePath extends `${infer L}/$${infer C}`
115
- ? PathParamMask<`${L}/${string}`>
116
- : TRoutePath
115
+ ? PathParamMask<`${L}/${string}`>
116
+ : TRoutePath
117
117
 
118
118
  export type Timeout = ReturnType<typeof setTimeout>
119
119
 
@@ -314,10 +314,10 @@ export type RouteFromIdOrRoute<
314
314
  > = T extends ParseRoute<TRouteTree>
315
315
  ? T
316
316
  : T extends RouteIds<TRouteTree>
317
- ? RoutesById<TRouteTree>[T]
318
- : T extends string
319
- ? RouteIds<TRouteTree>
320
- : never
317
+ ? RoutesById<TRouteTree>[T]
318
+ : T extends string
319
+ ? RouteIds<TRouteTree>
320
+ : never
321
321
 
322
322
  export function useRouteContext<
323
323
  TRouteTree extends AnyRoute = RegisteredRouter['routeTree'],
@@ -326,17 +326,9 @@ export function useRouteContext<
326
326
  TRouteContext = RouteById<TRouteTree, TFrom>['types']['allContext'],
327
327
  TSelected = TRouteContext,
328
328
  >(
329
- opts: StrictOrFrom<TFrom> & {
330
- select?: (search: TRouteContext) => TSelected
331
- },
329
+ opts: StrictOrFrom<TFrom>,
332
330
  ): TStrict extends true ? TSelected : TSelected | undefined {
333
- return useMatch({
334
- ...(opts as any),
335
- select: (match: RouteMatch) =>
336
- opts?.select
337
- ? opts.select(match.context as TRouteContext)
338
- : match.context,
339
- })
331
+ return useMatch(opts).context
340
332
  }
341
333
 
342
334
  export const useLayoutEffect =