Skip to content

Commit 732ad27

Browse files
committed
Fix Await usages outside of a router
1 parent cf2990e commit 732ad27

File tree

1 file changed

+24
-18
lines changed

1 file changed

+24
-18
lines changed

packages/react-router/lib/components.tsx

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1608,15 +1608,30 @@ export function Await<Resolve>({
16081608
resolve,
16091609
}: AwaitProps<Resolve>) {
16101610
let dataRouterContext = React.useContext(DataRouterContext);
1611-
let location = useLocation();
1612-
let params = useParams();
1611+
let dataRouterStateContext = React.useContext(DataRouterStateContext);
1612+
1613+
let onError = React.useCallback(
1614+
(error: unknown, errorInfo?: React.ErrorInfo) => {
1615+
if (
1616+
dataRouterContext &&
1617+
dataRouterContext.unstable_onError &&
1618+
dataRouterStateContext
1619+
) {
1620+
dataRouterContext.unstable_onError(error, {
1621+
location: dataRouterStateContext.location,
1622+
params: dataRouterStateContext.matches?.[0]?.params || {},
1623+
errorInfo,
1624+
});
1625+
}
1626+
},
1627+
[dataRouterContext, dataRouterStateContext],
1628+
);
1629+
16131630
return (
16141631
<AwaitErrorBoundary
16151632
resolve={resolve}
16161633
errorElement={errorElement}
1617-
unstable_onError={dataRouterContext?.unstable_onError}
1618-
location={location}
1619-
params={params}
1634+
onError={onError}
16201635
>
16211636
<ResolveAwait>{children}</ResolveAwait>
16221637
</AwaitErrorBoundary>
@@ -1625,10 +1640,8 @@ export function Await<Resolve>({
16251640

16261641
type AwaitErrorBoundaryProps = React.PropsWithChildren<{
16271642
errorElement?: React.ReactNode;
1628-
location: Location;
1629-
params: Params;
16301643
resolve: TrackedPromise | any;
1631-
unstable_onError?: unstable_ClientOnErrorFunction;
1644+
onError?: (error: unknown, errorInfo?: React.ErrorInfo) => void;
16321645
}>;
16331646

16341647
type AwaitErrorBoundaryState = {
@@ -1655,13 +1668,9 @@ class AwaitErrorBoundary extends React.Component<
16551668
}
16561669

16571670
componentDidCatch(error: any, errorInfo: React.ErrorInfo) {
1658-
if (this.props.unstable_onError) {
1671+
if (this.props.onError) {
16591672
// Log render errors
1660-
this.props.unstable_onError(error, {
1661-
location: this.props.location,
1662-
params: this.props.params,
1663-
errorInfo,
1664-
});
1673+
this.props.onError(error, errorInfo);
16651674
} else {
16661675
console.error(
16671676
"<Await> caught the following error during render",
@@ -1708,10 +1717,7 @@ class AwaitErrorBoundary extends React.Component<
17081717
Object.defineProperty(resolve, "_data", { get: () => data }),
17091718
(error: any) => {
17101719
// Log promise rejections
1711-
this.props.unstable_onError?.(error, {
1712-
location: this.props.location,
1713-
params: this.props.params,
1714-
});
1720+
this.props.onError?.(error);
17151721
Object.defineProperty(resolve, "_error", { get: () => error });
17161722
},
17171723
);

0 commit comments

Comments
 (0)