Skip to content

Commit fb71ac7

Browse files
committed
Lift error function out of error boundary in renderMatches
1 parent 732ad27 commit fb71ac7

File tree

1 file changed

+15
-10
lines changed

1 file changed

+15
-10
lines changed

packages/react-router/lib/hooks.tsx

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -983,12 +983,11 @@ const defaultErrorElement = <DefaultErrorComponent />;
983983

984984
type RenderErrorBoundaryProps = React.PropsWithChildren<{
985985
location: Location;
986-
params: Params;
987986
revalidation: RevalidationState;
988987
error: any;
989988
component: React.ReactNode;
990989
routeContext: RouteContextObject;
991-
unstable_onError: unstable_ClientOnErrorFunction | null;
990+
onError?: (error: unknown, errorInfo?: React.ErrorInfo) => void;
992991
}>;
993992

994993
type RenderErrorBoundaryState = {
@@ -1049,12 +1048,8 @@ export class RenderErrorBoundary extends React.Component<
10491048
}
10501049

10511050
componentDidCatch(error: any, errorInfo: React.ErrorInfo) {
1052-
if (this.props.unstable_onError) {
1053-
this.props.unstable_onError(error, {
1054-
location: this.props.location,
1055-
params: this.props.params,
1056-
errorInfo,
1057-
});
1051+
if (this.props.onError) {
1052+
this.props.onError(error, errorInfo);
10581053
} else {
10591054
console.error(
10601055
"React Router caught the following error during render",
@@ -1191,6 +1186,17 @@ export function _renderMatches(
11911186
}
11921187
}
11931188

1189+
let onError =
1190+
dataRouterState && unstable_onError
1191+
? (error: unknown, errorInfo?: React.ErrorInfo) => {
1192+
unstable_onError(error, {
1193+
location: dataRouterState.location,
1194+
params: dataRouterState.matches?.[0]?.params ?? {},
1195+
errorInfo,
1196+
});
1197+
}
1198+
: undefined;
1199+
11941200
return renderedMatches.reduceRight(
11951201
(outlet, match, index) => {
11961202
// Only data routers handle errors/fallbacks
@@ -1260,13 +1266,12 @@ export function _renderMatches(
12601266
index === 0) ? (
12611267
<RenderErrorBoundary
12621268
location={dataRouterState.location}
1263-
params={match.params}
12641269
revalidation={dataRouterState.revalidation}
12651270
component={errorElement}
12661271
error={error}
12671272
children={getChildren()}
12681273
routeContext={{ outlet: null, matches, isDataRoute: true }}
1269-
unstable_onError={unstable_onError}
1274+
onError={onError}
12701275
/>
12711276
) : (
12721277
getChildren()

0 commit comments

Comments
 (0)