@tanstack/react-router 0.0.1-beta.241 → 0.0.1-beta.243

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.
@@ -21,7 +21,7 @@ import { Store } from '@tanstack/store';
21
21
  function CatchBoundary(props) {
22
22
  const errorComponent = props.errorComponent ?? ErrorComponent;
23
23
  return /*#__PURE__*/React.createElement(CatchBoundaryImpl, {
24
- resetKey: props.resetKey,
24
+ getResetKey: props.getResetKey,
25
25
  onCatch: props.onCatch,
26
26
  children: ({
27
27
  error
@@ -39,13 +39,18 @@ class CatchBoundaryImpl extends React.Component {
39
39
  state = {
40
40
  error: null
41
41
  };
42
+ static getDerivedStateFromProps(props) {
43
+ return {
44
+ resetKey: props.getResetKey()
45
+ };
46
+ }
42
47
  static getDerivedStateFromError(error) {
43
48
  return {
44
49
  error
45
50
  };
46
51
  }
47
52
  componentDidUpdate(prevProps, prevState) {
48
- if (prevState.error && prevProps.resetKey !== this.props.resetKey) {
53
+ if (prevState.error && prevState.resetKey !== this.state.resetKey) {
49
54
  this.setState({
50
55
  error: null
51
56
  });
@@ -608,11 +613,14 @@ function createRouteMask(opts) {
608
613
 
609
614
  //
610
615
 
616
+ const matchContext = /*#__PURE__*/React.createContext(undefined);
611
617
  function Matches() {
612
618
  const router = useRouter();
613
- const routerState = useRouterState();
614
- const matches = routerState.pendingMatches?.some(d => d.showPending) ? routerState.pendingMatches : routerState.matches;
615
- const locationKey = routerState.resolvedLocation.state.key;
619
+ const matchId = useRouterState({
620
+ select: s => {
621
+ return getRenderedMatches(s)[0]?.id;
622
+ }
623
+ });
616
624
  const route = router.routesById[rootRouteId];
617
625
  const errorComponent = React.useCallback(props => {
618
626
  return /*#__PURE__*/React.createElement(ErrorComponent, {
@@ -623,41 +631,38 @@ function Matches() {
623
631
  useParams: route.useParams
624
632
  });
625
633
  }, [route]);
626
- return /*#__PURE__*/React.createElement(matchesContext.Provider, {
627
- value: matches
634
+ return /*#__PURE__*/React.createElement(matchContext.Provider, {
635
+ value: matchId
628
636
  }, /*#__PURE__*/React.createElement(CatchBoundary, {
629
- resetKey: locationKey,
637
+ getResetKey: () => router.state.resolvedLocation.state?.key,
630
638
  errorComponent: errorComponent,
631
639
  onCatch: () => {
632
640
  warning(false, `Error in router! Consider setting an 'errorComponent' in your RootRoute! 👍`);
633
641
  }
634
- }, matches.length ? /*#__PURE__*/React.createElement(Match, {
635
- matches: matches
642
+ }, matchId ? /*#__PURE__*/React.createElement(Match, {
643
+ matchId: matchId
636
644
  }) : null));
637
645
  }
638
646
  function SafeFragment(props) {
639
647
  return /*#__PURE__*/React.createElement(React.Fragment, null, props.children);
640
648
  }
641
649
  function Match({
642
- matches
650
+ matchId
643
651
  }) {
644
- const {
645
- options,
646
- routesById
647
- } = useRouter();
648
- const match = matches[0];
649
- const routeId = match?.routeId;
650
- const route = routesById[routeId];
651
- useRouter();
652
- const locationKey = useRouterState().resolvedLocation.state?.key;
653
- const PendingComponent = route.options.pendingComponent ?? options.defaultPendingComponent;
652
+ const router = useRouter();
653
+ const routeId = useRouterState({
654
+ select: s => getRenderedMatches(s).find(d => d.id === matchId)?.routeId
655
+ });
656
+ invariant(routeId, `Could not find routeId for matchId "${matchId}". Please file an issue!`);
657
+ const route = router.routesById[routeId];
658
+ const PendingComponent = route.options.pendingComponent ?? router.options.defaultPendingComponent;
654
659
  const pendingElement = PendingComponent ? /*#__PURE__*/React.createElement(PendingComponent, {
655
660
  useMatch: route.useMatch,
656
661
  useRouteContext: route.useRouteContext,
657
662
  useSearch: route.useSearch,
658
663
  useParams: route.useParams
659
664
  }) : undefined;
660
- const routeErrorComponent = route.options.errorComponent ?? options.defaultErrorComponent ?? ErrorComponent;
665
+ const routeErrorComponent = route.options.errorComponent ?? router.options.defaultErrorComponent ?? ErrorComponent;
661
666
  const ResolvedSuspenseBoundary = route.options.wrapInSuspense ?? pendingElement ? React.Suspense : SafeFragment;
662
667
  const errorComponent = routeErrorComponent ? React.useCallback(props => {
663
668
  return /*#__PURE__*/React.createElement(routeErrorComponent, {
@@ -669,30 +674,33 @@ function Match({
669
674
  });
670
675
  }, [route]) : undefined;
671
676
  const ResolvedCatchBoundary = errorComponent ? CatchBoundary : SafeFragment;
672
- return /*#__PURE__*/React.createElement(matchesContext.Provider, {
673
- value: matches
677
+ return /*#__PURE__*/React.createElement(matchContext.Provider, {
678
+ value: matchId
674
679
  }, /*#__PURE__*/React.createElement(ResolvedSuspenseBoundary, {
675
680
  fallback: pendingElement
676
681
  }, /*#__PURE__*/React.createElement(ResolvedCatchBoundary, {
677
- resetKey: locationKey,
682
+ getResetKey: () => router.state.resolvedLocation.state?.key,
678
683
  errorComponent: errorComponent,
679
684
  onCatch: () => {
680
- warning(false, `Error in route match: ${match.id}`);
685
+ warning(false, `Error in route match: ${matchId}`);
681
686
  }
682
687
  }, /*#__PURE__*/React.createElement(MatchInner, {
683
- match: match,
688
+ matchId: matchId,
684
689
  pendingElement: pendingElement
685
690
  }))));
686
691
  }
687
692
  function MatchInner({
688
- match,
693
+ matchId,
689
694
  pendingElement
690
695
  }) {
691
- const {
692
- options,
693
- routesById
694
- } = useRouter();
695
- const route = routesById[match.routeId];
696
+ const router = useRouter();
697
+ const routeId = useRouterState({
698
+ select: s => getRenderedMatches(s).find(d => d.id === matchId)?.routeId
699
+ });
700
+ const route = router.routesById[routeId];
701
+ const match = useRouterState({
702
+ select: s => pick(getRenderedMatches(s).find(d => d.id === matchId), ['status', 'error', 'showPending', 'loadPromise'])
703
+ });
696
704
  if (match.status === 'error') {
697
705
  throw match.error;
698
706
  }
@@ -703,7 +711,7 @@ function MatchInner({
703
711
  throw match.loadPromise;
704
712
  }
705
713
  if (match.status === 'success') {
706
- let comp = route.options.component ?? options.defaultComponent;
714
+ let comp = route.options.component ?? router.options.defaultComponent;
707
715
  if (comp) {
708
716
  return /*#__PURE__*/React.createElement(comp, {
709
717
  useMatch: route.useMatch,
@@ -718,12 +726,19 @@ function MatchInner({
718
726
  invariant(false, 'Idle routeMatch status encountered during rendering! You should never see this. File an issue!');
719
727
  }
720
728
  function Outlet() {
721
- const matches = React.useContext(matchesContext).slice(1);
722
- if (!matches[0]) {
729
+ const matchId = React.useContext(matchContext);
730
+ const childMatchId = useRouterState({
731
+ select: s => {
732
+ const matches = getRenderedMatches(s);
733
+ const index = matches.findIndex(d => d.id === matchId);
734
+ return matches[index + 1]?.id;
735
+ }
736
+ });
737
+ if (!childMatchId) {
723
738
  return null;
724
739
  }
725
740
  return /*#__PURE__*/React.createElement(Match, {
726
- matches: matches
741
+ matchId: childMatchId
727
742
  });
728
743
  }
729
744
  function useMatchRoute() {
@@ -750,46 +765,47 @@ function MatchRoute(props) {
750
765
  }
751
766
  return !!params ? props.children : null;
752
767
  }
768
+ function getRenderedMatches(state) {
769
+ return state.pendingMatches?.some(d => d.showPending) ? state.pendingMatches : state.matches;
770
+ }
753
771
  function useMatch(opts) {
754
- const nearestMatch = React.useContext(matchesContext)[0];
755
- const nearestMatchRouteId = nearestMatch?.routeId;
756
- const matchRouteId = useRouterState({
757
- select: state => {
758
- const matches = state.pendingMatches?.some(d => d.showPending) ? state.pendingMatches : state.matches;
759
- const match = opts?.from ? matches.find(d => d.routeId === opts?.from) : matches.find(d => d.id === nearestMatch.id);
760
- return match.routeId;
761
- }
762
- });
772
+ const router = useRouter();
773
+ const nearestMatchId = React.useContext(matchContext);
774
+ const nearestMatchRouteId = getRenderedMatches(router.state).find(d => d.id === nearestMatchId)?.routeId;
775
+ const matchRouteId = (() => {
776
+ const matches = getRenderedMatches(router.state);
777
+ const match = opts?.from ? matches.find(d => d.routeId === opts?.from) : matches.find(d => d.id === nearestMatchId);
778
+ return match.routeId;
779
+ })();
763
780
  if (opts?.strict ?? true) {
764
781
  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?`);
765
782
  }
766
783
  const matchSelection = useRouterState({
767
784
  select: state => {
768
- const matches = state.pendingMatches?.some(d => d.showPending) ? state.pendingMatches : state.matches;
769
- const match = opts?.from ? matches.find(d => d.routeId === opts?.from) : matches.find(d => d.id === nearestMatch.id);
785
+ const match = getRenderedMatches(state).find(d => d.id === nearestMatchId);
770
786
  invariant(match, `Could not find ${opts?.from ? `an active match from "${opts.from}"` : 'a nearest match!'}`);
771
787
  return opts?.select ? opts.select(match) : match;
772
788
  }
773
789
  });
774
790
  return matchSelection;
775
791
  }
776
- const matchesContext = /*#__PURE__*/React.createContext(null);
777
792
  function useMatches(opts) {
778
- const contextMatches = React.useContext(matchesContext);
793
+ const contextMatchId = React.useContext(matchContext);
779
794
  return useRouterState({
780
795
  select: state => {
781
- let matches = state.pendingMatches?.some(d => d.showPending) ? state.pendingMatches : state.matches;
782
- matches = matches.slice(matches.findIndex(d => d.id === contextMatches[0]?.id));
796
+ let matches = getRenderedMatches(state);
797
+ matches = matches.slice(matches.findIndex(d => d.id === contextMatchId));
783
798
  return opts?.select ? opts.select(matches) : matches;
784
799
  }
785
800
  });
786
801
  }
787
802
  function useLoaderData(opts) {
788
- const match = useMatch({
803
+ return useMatch({
789
804
  ...opts,
790
- select: undefined
805
+ select: s => {
806
+ return typeof opts.select === 'function' ? opts.select(s?.loaderData) : s?.loaderData;
807
+ }
791
808
  });
792
- return typeof opts.select === 'function' ? opts.select(match?.loaderData) : match?.loaderData;
793
809
  }
794
810
 
795
811
  const routerContext = /*#__PURE__*/React.createContext(null);
@@ -1509,7 +1525,7 @@ class Router {
1509
1525
  return [parentSearch, searchError];
1510
1526
  }
1511
1527
  })();
1512
- const interpolatedPath = interpolatePath(route.path, routeParams);
1528
+ const interpolatedPath = interpolatePath(route.fullPath, routeParams);
1513
1529
  const matchId = interpolatePath(route.id, routeParams, true) + (route.options.key?.({
1514
1530
  search: preMatchSearch,
1515
1531
  location: this.state.location
@@ -2504,12 +2520,13 @@ function useNavigate(defaultOpts) {
2504
2520
  const {
2505
2521
  navigate
2506
2522
  } = useRouter();
2507
- const match = useMatch({
2508
- strict: false
2523
+ const matchPathname = useMatch({
2524
+ strict: false,
2525
+ select: s => s.pathname
2509
2526
  });
2510
2527
  return React.useCallback(opts => {
2511
2528
  return navigate({
2512
- from: opts?.to ? match.pathname : undefined,
2529
+ from: opts?.to ? matchPathname : undefined,
2513
2530
  ...defaultOpts,
2514
2531
  ...opts
2515
2532
  });
@@ -2535,5 +2552,5 @@ function Navigate(props) {
2535
2552
  return null;
2536
2553
  }
2537
2554
 
2538
- export { Await, Block, CatchBoundary, CatchBoundaryImpl, ErrorComponent, FileRoute, Link, Match, MatchRoute, Matches, Navigate, Outlet, PathParamError, RootRoute, Route, Router, RouterProvider, ScrollRestoration, SearchParamError, cleanPath, componentTypes, createRouteMask, decode, deepEqual, defaultParseSearch, defaultStringifySearch, defer, encode, escapeJSON, functionalUpdate, getInitialRouterState, getRouteMatch, interpolatePath, isDehydratedDeferred, isPlainObject, isRedirect, isServer, joinPaths, last, lazyFn, lazyRouteComponent, matchByPath, matchPathname, matchesContext, parsePathname, parseSearchWith, pick, redirect, replaceEqualDeep, resolvePath, rootRouteId, rootRouteWithContext, routerContext, shallow, stringifySearchWith, trimPath, trimPathLeft, trimPathRight, typedNavigate, useAwaited, useBlocker, useElementScrollRestoration, useLayoutEffect$1 as useLayoutEffect, useLinkProps, useLoaderData, useMatch, useMatchRoute, useMatches, useNavigate, useParams, useRouteContext, useRouter, useRouterState, useScrollRestoration, useSearch, useStableCallback };
2555
+ export { Await, Block, CatchBoundary, CatchBoundaryImpl, ErrorComponent, FileRoute, Link, Match, MatchRoute, Matches, Navigate, Outlet, PathParamError, RootRoute, Route, Router, RouterProvider, ScrollRestoration, SearchParamError, cleanPath, componentTypes, createRouteMask, decode, deepEqual, defaultParseSearch, defaultStringifySearch, defer, encode, escapeJSON, functionalUpdate, getInitialRouterState, getRouteMatch, interpolatePath, isDehydratedDeferred, isPlainObject, isRedirect, isServer, joinPaths, last, lazyFn, lazyRouteComponent, matchByPath, matchContext, matchPathname, parsePathname, parseSearchWith, pick, redirect, replaceEqualDeep, resolvePath, rootRouteId, rootRouteWithContext, routerContext, shallow, stringifySearchWith, trimPath, trimPathLeft, trimPathRight, typedNavigate, useAwaited, useBlocker, useElementScrollRestoration, useLayoutEffect$1 as useLayoutEffect, useLinkProps, useLoaderData, useMatch, useMatchRoute, useMatches, useNavigate, useParams, useRouteContext, useRouter, useRouterState, useScrollRestoration, useSearch, useStableCallback };
2539
2556
  //# sourceMappingURL=index.js.map