@tanstack/react-router 0.0.1-beta.69 → 0.0.1-beta.70

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/src/index.tsx CHANGED
@@ -10,7 +10,6 @@ import {
10
10
  RouterOptions,
11
11
  RouteMatch,
12
12
  MatchRouteOptions,
13
- AnyRoute,
14
13
  AnyRoutesInfo,
15
14
  DefaultRoutesInfo,
16
15
  functionalUpdate,
@@ -23,12 +22,8 @@ import {
23
22
  ToOptions,
24
23
  invariant,
25
24
  Router,
26
- Expand,
27
- AnyContext,
28
25
  AnyRootRoute,
29
26
  RootRoute,
30
- AnySearchSchema,
31
- AnyPathParams,
32
27
  AnyRouteMatch,
33
28
  NavigateOptions,
34
29
  RouterConstructorOptions,
@@ -122,7 +117,7 @@ declare module '@tanstack/router' {
122
117
  interface FrameworkGenerics {
123
118
  Component: RouteComponent
124
119
  ErrorComponent: RouteComponent<{
125
- error: unknown
120
+ error: Error
126
121
  info: { componentStack: string }
127
122
  }>
128
123
  }
@@ -353,13 +348,21 @@ export function RouterProvider<
353
348
  React.useEffect(router.mount, [router])
354
349
 
355
350
  return (
356
- <>
357
- <routerContext.Provider value={{ router: router as any }}>
358
- <matchesContext.Provider value={[undefined!, ...currentMatches]}>
351
+ <routerContext.Provider value={{ router: router as any }}>
352
+ <matchesContext.Provider value={[undefined!, ...currentMatches]}>
353
+ <CatchBoundary
354
+ errorComponent={ErrorComponent}
355
+ onCatch={() => {
356
+ warning(
357
+ false,
358
+ `Error in router! Consider setting an 'errorComponent' in your RootRoute! 👍`,
359
+ )
360
+ }}
361
+ >
359
362
  <Outlet />
360
- </matchesContext.Provider>
361
- </routerContext.Provider>
362
- </>
363
+ </CatchBoundary>
364
+ </matchesContext.Provider>
365
+ </routerContext.Provider>
363
366
  )
364
367
  }
365
368
 
@@ -602,44 +605,31 @@ function SubOutlet({
602
605
  const errorComponent =
603
606
  match.errorComponent ?? router.options.defaultErrorComponent
604
607
 
608
+ const ResolvedSuspenseBoundary =
609
+ match.route.options.wrapInSuspense ?? true ? React.Suspense : SafeFragment
610
+ const ResolvedCatchBoundary = errorComponent ? CatchBoundary : SafeFragment
611
+
605
612
  return (
606
613
  <matchesContext.Provider value={matches}>
607
- {match.route.options.wrapInSuspense ?? true ? (
608
- <React.Suspense fallback={<PendingComponent />}>
609
- <CatchBoundary
610
- key={match.route.id}
611
- errorComponent={errorComponent}
612
- match={match as any}
613
- >
614
- <Inner match={match} />
615
- </CatchBoundary>
616
- </React.Suspense>
617
- ) : (
618
- <CatchBoundary
614
+ <ResolvedSuspenseBoundary fallback={<PendingComponent />}>
615
+ <ResolvedCatchBoundary
619
616
  key={match.route.id}
620
617
  errorComponent={errorComponent}
621
- match={match as any}
618
+ onCatch={() => {
619
+ warning(false, `Error in route match: ${match.id}`)
620
+ }}
622
621
  >
623
622
  <Inner match={match} />
624
- </CatchBoundary>
625
- )}
626
- {/* Provide a suffix suspense boundary to make sure the router is
627
- ready to be dehydrated on the server */}
628
- {/* {router.options.ssrFooter && match.id === rootRouteId ? (
629
- <React.Suspense fallback={null}>
630
- {(() => {
631
- if (router.store.pending) {
632
- throw router.navigationPromise
633
- }
634
-
635
- return router.options.ssrFooter()
636
- })()}
637
- </React.Suspense>
638
- ) : null} */}
623
+ </ResolvedCatchBoundary>
624
+ </ResolvedSuspenseBoundary>
639
625
  </matchesContext.Provider>
640
626
  )
641
627
  }
642
628
 
629
+ function SafeFragment(props: any) {
630
+ return <>{props.children}</>
631
+ }
632
+
643
633
  // This is the messiest thing ever... I'm either seriously tired (likely) or
644
634
  // there has to be a better way to reset error boundaries when the
645
635
  // router's location key changes.
@@ -647,14 +637,14 @@ function SubOutlet({
647
637
  class CatchBoundary extends React.Component<{
648
638
  children: any
649
639
  errorComponent: any
650
- match: RouteMatch
640
+ onCatch: (error: any, info: any) => void
651
641
  }> {
652
642
  state = {
653
643
  error: false,
654
644
  info: undefined,
655
645
  }
656
646
  componentDidCatch(error: any, info: any) {
657
- console.error(`Error in route match: ${this.props.match.id}`)
647
+ this.props.onCatch(error, info)
658
648
  console.error(error)
659
649
  this.setState({
660
650
  error,
@@ -682,7 +672,7 @@ function CatchBoundaryInner(props: {
682
672
  props.errorState,
683
673
  )
684
674
  const router = useRouterContext()
685
- const errorComponent = props.errorComponent ?? DefaultErrorBoundary
675
+ const errorComponent = props.errorComponent ?? ErrorComponent
686
676
  const prevKeyRef = React.useRef('' as any)
687
677
 
688
678
  React.useEffect(() => {
@@ -709,7 +699,7 @@ function CatchBoundaryInner(props: {
709
699
  return props.children
710
700
  }
711
701
 
712
- export function DefaultErrorBoundary({ error }: { error: any }) {
702
+ export function ErrorComponent({ error }: { error: any }) {
713
703
  return (
714
704
  <div style={{ padding: '.5rem', maxWidth: '100%' }}>
715
705
  <strong style={{ fontSize: '1.2rem' }}>Something went wrong!</strong>