react-router 6.5.0 → 6.6.0

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/CHANGELOG.md CHANGED
@@ -1,5 +1,13 @@
1
1
  # `react-router`
2
2
 
3
+ ## 6.6.0
4
+
5
+ ### Patch Changes
6
+
7
+ - Prevent `useLoaderData` usage in `errorElement` ([#9735](https://github.com/remix-run/react-router/pull/9735))
8
+ - Updated dependencies:
9
+ - `@remix-run/router@1.2.0`
10
+
3
11
  ## 6.5.0
4
12
 
5
13
  This release introduces support for [Optional Route Segments](https://github.com/remix-run/react-router/issues/9546). Now, adding a `?` to the end of any path segment will make that entire segment optional. This works for both static segments and dynamic parameters.
package/dist/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /**
2
- * React Router v6.5.0
2
+ * React Router v6.6.0
3
3
  *
4
4
  * Copyright (c) Remix Software Inc.
5
5
  *
@@ -644,10 +644,12 @@ class RenderErrorBoundary extends React.Component {
644
644
  }
645
645
 
646
646
  render() {
647
- return this.state.error ? /*#__PURE__*/React.createElement(RouteErrorContext.Provider, {
647
+ return this.state.error ? /*#__PURE__*/React.createElement(RouteContext.Provider, {
648
+ value: this.props.routeContext
649
+ }, /*#__PURE__*/React.createElement(RouteErrorContext.Provider, {
648
650
  value: this.state.error,
649
651
  children: this.props.component
650
- }) : this.props.children;
652
+ })) : this.props.children;
651
653
  }
652
654
 
653
655
  }
@@ -699,12 +701,13 @@ function _renderMatches(matches, parentMatches, dataRouterState) {
699
701
  let error = match.route.id ? errors == null ? void 0 : errors[match.route.id] : null; // Only data routers handle errors
700
702
 
701
703
  let errorElement = dataRouterState ? match.route.errorElement || /*#__PURE__*/React.createElement(DefaultErrorElement, null) : null;
704
+ let matches = parentMatches.concat(renderedMatches.slice(0, index + 1));
702
705
 
703
706
  let getChildren = () => /*#__PURE__*/React.createElement(RenderedRoute, {
704
707
  match: match,
705
708
  routeContext: {
706
709
  outlet,
707
- matches: parentMatches.concat(renderedMatches.slice(0, index + 1))
710
+ matches
708
711
  }
709
712
  }, error ? errorElement : match.route.element !== undefined ? match.route.element : outlet); // Only wrap in an error boundary within data router usages when we have an
710
713
  // errorElement on this route. Otherwise let it bubble up to an ancestor
@@ -715,7 +718,11 @@ function _renderMatches(matches, parentMatches, dataRouterState) {
715
718
  location: dataRouterState.location,
716
719
  component: errorElement,
717
720
  error: error,
718
- children: getChildren()
721
+ children: getChildren(),
722
+ routeContext: {
723
+ outlet: null,
724
+ matches
725
+ }
719
726
  }) : getChildren();
720
727
  }, null);
721
728
  }
@@ -752,6 +759,19 @@ function useDataRouterState(hookName) {
752
759
  !state ? process.env.NODE_ENV !== "production" ? invariant(false, getDataRouterConsoleError(hookName)) : invariant(false) : void 0;
753
760
  return state;
754
761
  }
762
+
763
+ function useRouteContext(hookName) {
764
+ let route = React.useContext(RouteContext);
765
+ !route ? process.env.NODE_ENV !== "production" ? invariant(false, getDataRouterConsoleError(hookName)) : invariant(false) : void 0;
766
+ return route;
767
+ }
768
+
769
+ function useCurrentRouteId(hookName) {
770
+ let route = useRouteContext(hookName);
771
+ let thisRoute = route.matches[route.matches.length - 1];
772
+ !thisRoute.route.id ? process.env.NODE_ENV !== "production" ? invariant(false, hookName + " can only be used on routes that contain a unique \"id\"") : invariant(false) : void 0;
773
+ return thisRoute.route.id;
774
+ }
755
775
  /**
756
776
  * Returns the current navigation, defaulting to an "idle" navigation when
757
777
  * no navigation is in progress
@@ -808,11 +828,14 @@ function useMatches() {
808
828
 
809
829
  function useLoaderData() {
810
830
  let state = useDataRouterState(DataRouterStateHook.UseLoaderData);
811
- let route = React.useContext(RouteContext);
812
- !route ? process.env.NODE_ENV !== "production" ? invariant(false, "useLoaderData must be used inside a RouteContext") : invariant(false) : void 0;
813
- let thisRoute = route.matches[route.matches.length - 1];
814
- !thisRoute.route.id ? process.env.NODE_ENV !== "production" ? invariant(false, "useLoaderData can only be used on routes that contain a unique \"id\"") : invariant(false) : void 0;
815
- return state.loaderData[thisRoute.route.id];
831
+ let routeId = useCurrentRouteId(DataRouterStateHook.UseLoaderData);
832
+
833
+ if (state.errors && state.errors[routeId] != null) {
834
+ console.error("You cannot `useLoaderData` in an errorElement (routeId: " + routeId + ")");
835
+ return undefined;
836
+ }
837
+
838
+ return state.loaderData[routeId];
816
839
  }
817
840
  /**
818
841
  * Returns the loaderData for the given routeId
@@ -843,18 +866,15 @@ function useRouteError() {
843
866
 
844
867
  let error = React.useContext(RouteErrorContext);
845
868
  let state = useDataRouterState(DataRouterStateHook.UseRouteError);
846
- let route = React.useContext(RouteContext);
847
- let thisRoute = route.matches[route.matches.length - 1]; // If this was a render error, we put it in a RouteError context inside
869
+ let routeId = useCurrentRouteId(DataRouterStateHook.UseRouteError); // If this was a render error, we put it in a RouteError context inside
848
870
  // of RenderErrorBoundary
849
871
 
850
872
  if (error) {
851
873
  return error;
852
- }
874
+ } // Otherwise look for errors from our data router state
853
875
 
854
- !route ? process.env.NODE_ENV !== "production" ? invariant(false, "useRouteError must be used inside a RouteContext") : invariant(false) : void 0;
855
- !thisRoute.route.id ? process.env.NODE_ENV !== "production" ? invariant(false, "useRouteError can only be used on routes that contain a unique \"id\"") : invariant(false) : void 0; // Otherwise look for errors from our data router state
856
876
 
857
- return (_state$errors = state.errors) == null ? void 0 : _state$errors[thisRoute.route.id];
877
+ return (_state$errors = state.errors) == null ? void 0 : _state$errors[routeId];
858
878
  }
859
879
  /**
860
880
  * Returns the happy-path data from the nearest ancestor <Await /> value