@tanstack/react-router 0.0.1-alpha.5 → 0.0.1-alpha.7

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-alpha.5",
4
+ "version": "0.0.1-alpha.7",
5
5
  "license": "MIT",
6
6
  "repository": "tanstack/router",
7
7
  "homepage": "https://react-router.tanstack.com/",
@@ -40,7 +40,7 @@
40
40
  },
41
41
  "dependencies": {
42
42
  "@babel/runtime": "^7.16.7",
43
- "@tanstack/router-core": "0.0.1-alpha.5",
43
+ "@tanstack/router-core": "0.0.1-alpha.7",
44
44
  "use-sync-external-store": "^1.2.0"
45
45
  },
46
46
  "devDependencies": {
package/src/index.tsx CHANGED
@@ -7,6 +7,7 @@ import {
7
7
  RootRouteId,
8
8
  rootRouteId,
9
9
  Router,
10
+ RouterState,
10
11
  } from '@tanstack/router-core'
11
12
  import {
12
13
  warning,
@@ -49,6 +50,7 @@ declare module '@tanstack/router-core' {
49
50
  Route<TAllRouteInfo, TAllRouteInfo['routeInfoById'][RootRouteId]>,
50
51
  'linkProps' | 'Link' | 'MatchRoute'
51
52
  > {
53
+ useState: () => RouterState
52
54
  useRoute: <TId extends keyof TAllRouteInfo['routeInfoById']>(
53
55
  routeId: TId,
54
56
  ) => Route<TAllRouteInfo, TAllRouteInfo['routeInfoById'][TId]>
@@ -237,11 +239,11 @@ export function createReactRouter<
237
239
 
238
240
  // Get the active props
239
241
  const resolvedActiveProps: React.HTMLAttributes<HTMLAnchorElement> =
240
- isActive ? functionalUpdate(activeProps) ?? {} : {}
242
+ isActive ? functionalUpdate(activeProps, {}) ?? {} : {}
241
243
 
242
244
  // Get the inactive props
243
245
  const resolvedInactiveProps: React.HTMLAttributes<HTMLAnchorElement> =
244
- isActive ? {} : functionalUpdate(inactiveProps) ?? {}
246
+ isActive ? {} : functionalUpdate(inactiveProps, {}) ?? {}
245
247
 
246
248
  return {
247
249
  ...resolvedActiveProps,
@@ -303,8 +305,6 @@ export function createReactRouter<
303
305
  caseSensitive,
304
306
  })
305
307
 
306
- // useRouterSubscription(router)
307
-
308
308
  if (!params) {
309
309
  return null
310
310
  }
@@ -319,7 +319,14 @@ export function createReactRouter<
319
319
  const coreRouter = createRouter<TRouteConfig>({
320
320
  ...opts,
321
321
  createRouter: (router) => {
322
- const routerExt: Pick<Router<any, any>, 'useRoute' | 'useMatch'> = {
322
+ const routerExt: Pick<
323
+ Router<any, any>,
324
+ 'useRoute' | 'useMatch' | 'useState'
325
+ > = {
326
+ useState: () => {
327
+ useRouterSubscription(router)
328
+ return router.state
329
+ },
323
330
  useRoute: (routeId) => {
324
331
  const route = router.getRoute(routeId)
325
332
  useRouterSubscription(router)
@@ -332,6 +339,8 @@ export function createReactRouter<
332
339
  return route
333
340
  },
334
341
  useMatch: (routeId) => {
342
+ useRouterSubscription(router)
343
+
335
344
  invariant(
336
345
  routeId !== rootRouteId,
337
346
  `"${rootRouteId}" cannot be used with useMatch! Did you mean to useRoute("${rootRouteId}")?`,
@@ -358,8 +367,6 @@ export function createReactRouter<
358
367
  })' instead?`,
359
368
  )
360
369
 
361
- useRouterSubscription(router)
362
-
363
370
  if (!match) {
364
371
  invariant('Match not found!')
365
372
  }
@@ -397,14 +404,11 @@ export function RouterProvider<
397
404
  >({ children, router, ...rest }: RouterProps<TRouteConfig, TAllRouteInfo>) {
398
405
  router.update(rest)
399
406
 
400
- useSyncExternalStore(
401
- (cb) => router.subscribe(() => cb()),
402
- () => router.state,
403
- )
407
+ useRouterSubscription(router)
404
408
 
405
409
  useLayoutEffect(() => {
406
- router.mount()
407
- }, [])
410
+ return router.mount()
411
+ }, [router])
408
412
 
409
413
  return (
410
414
  <routerContext.Provider value={{ router }}>
@@ -415,7 +419,7 @@ export function RouterProvider<
415
419
  )
416
420
  }
417
421
 
418
- export function useRouter(): Router {
422
+ function useRouter(): Router {
419
423
  const value = React.useContext(routerContext)
420
424
  warning(!value, 'useRouter must be used inside a <Router> component!')
421
425
 
@@ -424,21 +428,21 @@ export function useRouter(): Router {
424
428
  return value.router as Router
425
429
  }
426
430
 
427
- export function useMatches(): RouteMatch[] {
431
+ function useMatches(): RouteMatch[] {
428
432
  return React.useContext(matchesContext)
429
433
  }
430
434
 
431
- export function useParentMatches(): RouteMatch[] {
432
- const router = useRouter()
433
- const match = useMatch()
434
- const matches = router.state.matches
435
- return matches.slice(
436
- 0,
437
- matches.findIndex((d) => d.matchId === match.matchId) - 1,
438
- )
439
- }
440
-
441
- export function useMatch<T>(): RouteMatch {
435
+ // function useParentMatches(): RouteMatch[] {
436
+ // const router = useRouter()
437
+ // const match = useMatch()
438
+ // const matches = router.state.matches
439
+ // return matches.slice(
440
+ // 0,
441
+ // matches.findIndex((d) => d.matchId === match.matchId) - 1,
442
+ // )
443
+ // }
444
+
445
+ function useMatch<T>(): RouteMatch {
442
446
  return useMatches()?.[0] as RouteMatch
443
447
  }
444
448
 
@@ -450,7 +454,7 @@ export function Outlet() {
450
454
 
451
455
  if (!childMatch) return null
452
456
 
453
- const element = (((): React.ReactNode => {
457
+ const element = ((): React.ReactNode => {
454
458
  if (!childMatch) {
455
459
  return null
456
460
  }
@@ -470,7 +474,7 @@ export function Outlet() {
470
474
  throw childMatch.error
471
475
  }
472
476
 
473
- return <DefaultCatchBoundary error={childMatch.error} />
477
+ return <DefaultErrorBoundary error={childMatch.error} />
474
478
  }
475
479
 
476
480
  if (childMatch.status === 'loading' || childMatch.status === 'idle') {
@@ -487,7 +491,7 @@ export function Outlet() {
487
491
  }
488
492
 
489
493
  return (childMatch.__.element as any) ?? router.options.defaultElement
490
- })() as JSX.Element) ?? <Outlet />
494
+ })() as JSX.Element
491
495
 
492
496
  const catchElement =
493
497
  childMatch?.options.catchElement ?? router.options.defaultCatchElement
@@ -521,7 +525,7 @@ class CatchBoundary extends React.Component<{
521
525
  })
522
526
  }
523
527
  render() {
524
- const catchElement = this.props.catchElement ?? DefaultCatchBoundary
528
+ const catchElement = this.props.catchElement ?? DefaultErrorBoundary
525
529
 
526
530
  if (this.state.error) {
527
531
  return typeof catchElement === 'function'
@@ -533,7 +537,7 @@ class CatchBoundary extends React.Component<{
533
537
  }
534
538
  }
535
539
 
536
- export function DefaultCatchBoundary({ error }: { error: any }) {
540
+ export function DefaultErrorBoundary({ error }: { error: any }) {
537
541
  return (
538
542
  <div style={{ padding: '.5rem', maxWidth: '100%' }}>
539
543
  <strong style={{ fontSize: '1.2rem' }}>Something went wrong!</strong>
@@ -571,3 +575,5 @@ export function DefaultCatchBoundary({ error }: { error: any }) {
571
575
  </div>
572
576
  )
573
577
  }
578
+
579
+ // TODO: Add prompt