Skip to content

Commit

Permalink
feat: pass error to layout
Browse files Browse the repository at this point in the history
  • Loading branch information
Mikhail Tsipotan committed Feb 29, 2024
1 parent d25e4ef commit b288524
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions src/components/routes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export const Routes: React.FC<RoutesProps> = ({

const [currentLocation, setCurrentLocation] = useState(location)
const [routeData, setRouteData] = useState(data)
const [error, setError] = useState<Error | null>(null)

const isLoading = location !== currentLocation
const isWaitingMode = transition === TransitionMode.WAIT_FOR_DATA
Expand All @@ -62,15 +63,22 @@ export const Routes: React.FC<RoutesProps> = ({
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) {
Expand All @@ -84,7 +92,7 @@ export const Routes: React.FC<RoutesProps> = ({

return (
<Suspense fallback={Fallback ? <Fallback /> : undefined}>
<Layout {...rest}>
<Layout {...rest} error={error}>
<BaseRoutes location={routerLocation}>
{routeData?.redirect && <Navigate to={routeData.redirect} />}
{routes.map(({path, Component, Fallback, ...routeProps}) => (
Expand Down

0 comments on commit b288524

Please sign in to comment.