Skip to content

Commit c339e6c

Browse files
committed
add errorElement
1 parent c5f96a8 commit c339e6c

File tree

2 files changed

+33
-18
lines changed

2 files changed

+33
-18
lines changed

exercises/99.final/01.solution.final/src/routes.tsx

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ import { AboutRoute } from './routes/app/marketing/about.tsx'
99
import { HomepageRoute } from './routes/app/marketing/homepage.tsx'
1010
import { MarketingLayout } from './routes/app/marketing/layout.tsx'
1111
import { NewRecipientRoute } from './routes/app/new-recipient.tsx'
12-
import { RecipientRoute } from './routes/app/recipient.tsx'
12+
import {
13+
RecipientErrorBoundary,
14+
RecipientRoute,
15+
} from './routes/app/recipient.tsx'
1316
import { RecipientsRoute } from './routes/app/recipients.tsx'
1417
import { SignupRoute } from './routes/signup.tsx'
1518

@@ -23,7 +26,11 @@ export const router = createBrowserRouter(
2326
</Route>
2427
<Route path="recipients" element={<RecipientsRoute />} />
2528
<Route path="recipients/new" element={<NewRecipientRoute />} />
26-
<Route path="recipients/:id" element={<RecipientRoute />} />
29+
<Route
30+
path="recipients/:id"
31+
element={<RecipientRoute />}
32+
errorElement={<RecipientErrorBoundary />}
33+
/>
2734
</Route>
2835
<Route path="/signup" element={<SignupRoute />} />
2936
<Route path="*" element={<NotFoundRoute />} />

exercises/99.final/01.solution.final/src/routes/app/recipient.tsx

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useParams, Link } from 'react-router'
1+
import { useParams, Link, useRouteError } from 'react-router'
22
import { Button } from '#src/components/button.tsx'
33
import { Icon } from '#src/components/icon.tsx'
44
import data from '#src/data.json'
@@ -9,21 +9,7 @@ export function RecipientRoute() {
99
const { id } = useParams()
1010
const recipient = recipients.find((r) => r.id === id)
1111

12-
if (!recipient) {
13-
return (
14-
<div className="container mx-auto mt-4 flex flex-col gap-8">
15-
<div className="bg-danger-background rounded-sm p-4">
16-
<p className="text-danger-foreground">Recipient not found</p>
17-
</div>
18-
<Link
19-
to="/recipients"
20-
className="text-link hover:text-link-hover block text-center hover:underline"
21-
>
22-
Back to recipients
23-
</Link>
24-
</div>
25-
)
26-
}
12+
if (!recipient) throw new Error(`Recipient with ID of "${id}" not found`)
2713

2814
return (
2915
<div className="flex min-h-0 flex-grow flex-col md:flex-row">
@@ -164,3 +150,25 @@ export function RecipientRoute() {
164150
</div>
165151
)
166152
}
153+
154+
export function RecipientErrorBoundary() {
155+
const error = useRouteError()
156+
157+
return (
158+
<div className="container mx-auto mt-4 flex flex-col gap-8 px-8">
159+
<div className="bg-danger-background rounded-sm p-4">
160+
<p className="text-danger-foreground">
161+
{error instanceof Error ? error.message : 'Unknown error'}
162+
</p>
163+
</div>
164+
<Link
165+
to="/recipients"
166+
className="text-link hover:text-link-hover block text-center hover:underline"
167+
>
168+
<Icon name="ArrowLeft" className="inline-block">
169+
Back to recipients
170+
</Icon>
171+
</Link>
172+
</div>
173+
)
174+
}

0 commit comments

Comments
 (0)