Skip to content
Source
Sign inSign up
TanStack/router
Open History to see the last commit

error.tsx

42 lines1.1 KB

1import { Await, createFileRoute } from '@tanstack/react-router'
2import * as React from 'react'
3​
4async function loadData() {
5 await new Promise((r) => setTimeout(r, 2000))
6 if (Math.random() > 0.5) throw new Error('Random error in streaming!')
7 return 'Hello!'
8}
9​
10export const Route = createFileRoute('/error')({
11 component: ErrorComponent,
12 loader: () => {
13 if (Math.random() > 0.5) throw new Error('Random error!')
14 return {
15 deferredData: loadData(),
16 }
17 },
18 pendingComponent: () => <p>Loading..</p>,
19 wrapInSuspense: true,
20 errorComponent: ({ error }) => {
21 return (
22 <div className="p-2">
23 <h3>Caught: {error.message}</h3>
24 <p>(This page has a 75% chance of throwing an error)</p>
25 </div>
26 )
27 },
28})
29​
30function ErrorComponent() {
31 return (
32 <div className="p-2">
33 <h3>
34 The loader of this page will have a 75% chance of throwing an error!
35 </h3>
36​
37 <Await promise={Route.useLoaderData().deferredData}>
38 {(data) => <p>Streaming data loaded successfully: {data}</p>}
39 </Await>
40 </div>
41 )