From b2885246ed5b72d122db273e23c986d1d59f7af3 Mon Sep 17 00:00:00 2001 From: Mikhail Tsipotan Date: Thu, 29 Feb 2024 19:09:57 +0300 Subject: [PATCH] feat: pass error to layout --- src/components/routes.tsx | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/src/components/routes.tsx b/src/components/routes.tsx index 2fea2b3..954fa70 100644 --- a/src/components/routes.tsx +++ b/src/components/routes.tsx @@ -41,6 +41,7 @@ export const Routes: React.FC = ({ const [currentLocation, setCurrentLocation] = useState(location) const [routeData, setRouteData] = useState(data) + const [error, setError] = useState(null) const isLoading = location !== currentLocation const isWaitingMode = transition === TransitionMode.WAIT_FOR_DATA @@ -62,15 +63,22 @@ export const Routes: React.FC = ({ window.scrollTo(0, 0) } - const {data, meta = {}} = await loadRouteData({pathname, routes, context}) + try { + const {data, meta = {}} = + (await loadRouteData({pathname, routes, context})) ?? {} - if (scrollToTop && isBlockedMode) { - window.scrollTo(0, 0) - } + if (scrollToTop && isBlockedMode) { + window.scrollTo(0, 0) + } - setRouteData((previousData) => ({...previousData, ...data})) - setCurrentLocation(location) - onChangeMetaData?.(meta) + setRouteData((previousData) => ({...previousData, ...data})) + setCurrentLocation(location) + onChangeMetaData?.(meta) + } catch (error) { + if (error instanceof Error) { + setError(error) + } + } } if (isLoading) { @@ -84,7 +92,7 @@ export const Routes: React.FC = ({ return ( : undefined}> - + {routeData?.redirect && } {routes.map(({path, Component, Fallback, ...routeProps}) => (