react-router 7.16.0 → 7.17.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +9 -1
- package/dist/development/{browser-nIQ4Nsyi.d.mts → browser-CGcs-0pD.d.mts} +1 -1
- package/dist/development/{chunk-QUQL4437.mjs → chunk-6CSD65Y2.mjs} +2 -2
- package/dist/{production/chunk-NALGHHKE.mjs → development/chunk-ASILSGTR.mjs} +2 -2
- package/dist/development/{chunk-SRID2YZ2.js → chunk-KFNXW4AL.js} +1 -1
- package/dist/development/{chunk-XEJDWL2B.js → chunk-PBLBZ3QU.js} +7 -7
- package/dist/{production/chunk-SKEDDLRM.js → development/chunk-PULC7NLK.js} +99 -99
- package/dist/development/{context-m8rizgnE.d.mts → context-CmHpk1Ws.d.mts} +1 -1
- package/dist/development/dom-export.d.mts +3 -3
- package/dist/development/dom-export.d.ts +1 -1
- package/dist/development/dom-export.js +28 -28
- package/dist/development/dom-export.mjs +3 -3
- package/dist/development/{index-react-server-client-BLiUx67a.d.ts → index-react-server-client-CwU9bE5R.d.ts} +1 -1
- package/dist/development/{index-react-server-client-CdKROblb.d.mts → index-react-server-client-DPrDrCew.d.mts} +1 -1
- package/dist/development/index-react-server-client.d.mts +2 -2
- package/dist/development/index-react-server-client.d.ts +1 -1
- package/dist/development/index-react-server-client.js +4 -4
- package/dist/development/index-react-server-client.mjs +2 -2
- package/dist/development/index-react-server.js +1 -1
- package/dist/development/index-react-server.mjs +1 -1
- package/dist/development/index.d.mts +6 -6
- package/dist/development/index.d.ts +2 -2
- package/dist/development/index.js +85 -85
- package/dist/development/index.mjs +3 -3
- package/dist/development/lib/types/internal.js +1 -1
- package/dist/development/lib/types/internal.mjs +1 -1
- package/dist/production/{browser-nIQ4Nsyi.d.mts → browser-CGcs-0pD.d.mts} +1 -1
- package/dist/{development/chunk-S54KXAEJ.mjs → production/chunk-5TQZEVD5.mjs} +2 -2
- package/dist/production/{chunk-EAQNHM3N.js → chunk-CTIXC7EV.js} +7 -7
- package/dist/{development/chunk-IBI7OMNB.js → production/chunk-EN242BO4.js} +99 -99
- package/dist/production/{chunk-Q65P7S7Y.mjs → chunk-OSYEOCBT.mjs} +2 -2
- package/dist/production/{chunk-Y7DNFQZP.js → chunk-RTRY3JFT.js} +1 -1
- package/dist/production/{context-m8rizgnE.d.mts → context-CmHpk1Ws.d.mts} +1 -1
- package/dist/production/dom-export.d.mts +3 -3
- package/dist/production/dom-export.d.ts +1 -1
- package/dist/production/dom-export.js +28 -28
- package/dist/production/dom-export.mjs +3 -3
- package/dist/production/{index-react-server-client-BLiUx67a.d.ts → index-react-server-client-CwU9bE5R.d.ts} +1 -1
- package/dist/production/{index-react-server-client-CdKROblb.d.mts → index-react-server-client-DPrDrCew.d.mts} +1 -1
- package/dist/production/index-react-server-client.d.mts +2 -2
- package/dist/production/index-react-server-client.d.ts +1 -1
- package/dist/production/index-react-server-client.js +4 -4
- package/dist/production/index-react-server-client.mjs +2 -2
- package/dist/production/index-react-server.js +1 -1
- package/dist/production/index-react-server.mjs +1 -1
- package/dist/production/index.d.mts +6 -6
- package/dist/production/index.d.ts +2 -2
- package/dist/production/index.js +85 -85
- package/dist/production/index.mjs +3 -3
- package/dist/production/lib/types/internal.js +1 -1
- package/dist/production/lib/types/internal.mjs +1 -1
- package/docs/explanation/backend-for-frontend.md +50 -0
- package/docs/explanation/code-splitting.md +61 -0
- package/docs/explanation/concurrency.md +135 -0
- package/docs/explanation/form-vs-fetcher.md +292 -0
- package/docs/explanation/hot-module-replacement.md +137 -0
- package/docs/explanation/hydration.md +14 -0
- package/docs/explanation/index-query-param.md +86 -0
- package/docs/explanation/index.md +4 -0
- package/docs/explanation/lazy-route-discovery.md +78 -0
- package/docs/explanation/location.md +6 -0
- package/docs/explanation/progressive-enhancement.md +150 -0
- package/docs/explanation/race-conditions.md +88 -0
- package/docs/explanation/react-transitions.md +160 -0
- package/docs/explanation/route-matching.md +7 -0
- package/docs/explanation/server-client-execution.md +4 -0
- package/docs/explanation/sessions-and-cookies.md +465 -0
- package/docs/explanation/special-files.md +16 -0
- package/docs/explanation/state-management.md +524 -0
- package/docs/explanation/styling.md +87 -0
- package/docs/explanation/type-safety.md +82 -0
- package/docs/how-to/accessibility.md +44 -0
- package/docs/how-to/client-data.md +199 -0
- package/docs/how-to/data-strategy.md +317 -0
- package/docs/how-to/error-boundary.md +231 -0
- package/docs/how-to/error-reporting.md +142 -0
- package/docs/how-to/fetchers.md +307 -0
- package/docs/how-to/file-route-conventions.md +410 -0
- package/docs/how-to/file-uploads.md +217 -0
- package/docs/how-to/form-validation.md +120 -0
- package/docs/how-to/headers.md +164 -0
- package/docs/how-to/index.md +4 -0
- package/docs/how-to/instrumentation.md +556 -0
- package/docs/how-to/meta.md +40 -0
- package/docs/how-to/middleware.md +763 -0
- package/docs/how-to/navigation-blocking.md +233 -0
- package/docs/how-to/optimize-revalidation.md +12 -0
- package/docs/how-to/pre-rendering.md +225 -0
- package/docs/how-to/presets.md +103 -0
- package/docs/how-to/react-server-components.md +899 -0
- package/docs/how-to/resource-routes.md +126 -0
- package/docs/how-to/route-module-type-safety.md +100 -0
- package/docs/how-to/search-params.md +4 -0
- package/docs/how-to/security.md +30 -0
- package/docs/how-to/server-bundles.md +66 -0
- package/docs/how-to/spa.md +120 -0
- package/docs/how-to/status.md +63 -0
- package/docs/how-to/suspense.md +132 -0
- package/docs/how-to/using-handle.md +117 -0
- package/docs/how-to/view-transitions.md +237 -0
- package/docs/how-to/webhook.md +50 -0
- package/docs/index.md +39 -0
- package/docs/start/data/actions.md +138 -0
- package/docs/start/data/custom.md +198 -0
- package/docs/start/data/data-loading.md +44 -0
- package/docs/start/data/index.md +4 -0
- package/docs/start/data/installation.md +52 -0
- package/docs/start/data/navigating.md +12 -0
- package/docs/start/data/pending-ui.md +12 -0
- package/docs/start/data/route-object.md +268 -0
- package/docs/start/data/routing.md +281 -0
- package/docs/start/data/testing.md +8 -0
- package/docs/start/declarative/index.md +4 -0
- package/docs/start/declarative/installation.md +43 -0
- package/docs/start/declarative/navigating.md +133 -0
- package/docs/start/declarative/routing.md +237 -0
- package/docs/start/declarative/url-values.md +65 -0
- package/docs/start/framework/actions.md +174 -0
- package/docs/start/framework/data-loading.md +201 -0
- package/docs/start/framework/deploying.md +96 -0
- package/docs/start/framework/index.md +4 -0
- package/docs/start/framework/installation.md +41 -0
- package/docs/start/framework/navigating.md +182 -0
- package/docs/start/framework/pending-ui.md +142 -0
- package/docs/start/framework/rendering.md +59 -0
- package/docs/start/framework/route-module.md +527 -0
- package/docs/start/framework/routing.md +362 -0
- package/docs/start/framework/testing.md +133 -0
- package/docs/start/index.md +4 -0
- package/docs/start/modes.md +201 -0
- package/docs/upgrading/component-routes.md +363 -0
- package/docs/upgrading/future.md +280 -0
- package/docs/upgrading/index.md +4 -0
- package/docs/upgrading/remix.md +403 -0
- package/docs/upgrading/router-provider.md +442 -0
- package/docs/upgrading/v6.md +382 -0
- package/package.json +2 -1
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Error Boundaries
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
# Error Boundaries
|
|
6
|
+
|
|
7
|
+
[MODES: framework, data]
|
|
8
|
+
|
|
9
|
+
<br/>
|
|
10
|
+
<br/>
|
|
11
|
+
|
|
12
|
+
To avoid rendering an empty page to users, route modules will automatically catch errors in your code and render the closest `ErrorBoundary`.
|
|
13
|
+
|
|
14
|
+
Error boundaries are not intended for rendering form validation errors or error reporting. Please see [Form Validation](./form-validation) and [Error Reporting](./error-reporting) instead.
|
|
15
|
+
|
|
16
|
+
## 1. Add a root error boundary
|
|
17
|
+
|
|
18
|
+
All applications should at a minimum export a root error boundary. This one handles the three main cases:
|
|
19
|
+
|
|
20
|
+
- Thrown `data` with a status code and text
|
|
21
|
+
- Instances of errors with a stack trace
|
|
22
|
+
- Randomly thrown values
|
|
23
|
+
|
|
24
|
+
### Framework Mode
|
|
25
|
+
|
|
26
|
+
[modes: framework]
|
|
27
|
+
|
|
28
|
+
In [Framework Mode][picking-a-mode], errors are passed to the route-level error boundary as a prop (see [`Route.ErrorBoundaryProps`][type-safety]), so you don't need to use a hook to grab it:
|
|
29
|
+
|
|
30
|
+
```tsx filename=root.tsx lines=[1,3-5]
|
|
31
|
+
import { Route } from "./+types/root";
|
|
32
|
+
|
|
33
|
+
export function ErrorBoundary({
|
|
34
|
+
error,
|
|
35
|
+
}: Route.ErrorBoundaryProps) {
|
|
36
|
+
if (isRouteErrorResponse(error)) {
|
|
37
|
+
return (
|
|
38
|
+
<>
|
|
39
|
+
<h1>
|
|
40
|
+
{error.status} {error.statusText}
|
|
41
|
+
</h1>
|
|
42
|
+
<p>{error.data}</p>
|
|
43
|
+
</>
|
|
44
|
+
);
|
|
45
|
+
} else if (error instanceof Error) {
|
|
46
|
+
return (
|
|
47
|
+
<div>
|
|
48
|
+
<h1>Error</h1>
|
|
49
|
+
<p>{error.message}</p>
|
|
50
|
+
<p>The stack trace is:</p>
|
|
51
|
+
<pre>{error.stack}</pre>
|
|
52
|
+
</div>
|
|
53
|
+
);
|
|
54
|
+
} else {
|
|
55
|
+
return <h1>Unknown Error</h1>;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
### Data Mode
|
|
61
|
+
|
|
62
|
+
[modes: data]
|
|
63
|
+
|
|
64
|
+
In [Data Mode][picking-a-mode], the `ErrorBoundary` doesn't receive props, so you can access it via `useRouteError`:
|
|
65
|
+
|
|
66
|
+
```tsx lines=[1,6,16]
|
|
67
|
+
import { useRouteError } from "react-router";
|
|
68
|
+
|
|
69
|
+
let router = createBrowserRouter([
|
|
70
|
+
{
|
|
71
|
+
path: "/",
|
|
72
|
+
ErrorBoundary: RootErrorBoundary,
|
|
73
|
+
Component: Root,
|
|
74
|
+
},
|
|
75
|
+
]);
|
|
76
|
+
|
|
77
|
+
function Root() {
|
|
78
|
+
/* ... */
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function RootErrorBoundary() {
|
|
82
|
+
let error = useRouteError();
|
|
83
|
+
if (isRouteErrorResponse(error)) {
|
|
84
|
+
return (
|
|
85
|
+
<>
|
|
86
|
+
<h1>
|
|
87
|
+
{error.status} {error.statusText}
|
|
88
|
+
</h1>
|
|
89
|
+
<p>{error.data}</p>
|
|
90
|
+
</>
|
|
91
|
+
);
|
|
92
|
+
} else if (error instanceof Error) {
|
|
93
|
+
return (
|
|
94
|
+
<div>
|
|
95
|
+
<h1>Error</h1>
|
|
96
|
+
<p>{error.message}</p>
|
|
97
|
+
<p>The stack trace is:</p>
|
|
98
|
+
<pre>{error.stack}</pre>
|
|
99
|
+
</div>
|
|
100
|
+
);
|
|
101
|
+
} else {
|
|
102
|
+
return <h1>Unknown Error</h1>;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
## 2. Write a bug
|
|
108
|
+
|
|
109
|
+
[modes: framework,data]
|
|
110
|
+
|
|
111
|
+
It's not recommended to intentionally throw errors to force the error boundary to render as a means of control flow. Error Boundaries are primarily for catching unintentional errors in your code.
|
|
112
|
+
|
|
113
|
+
```tsx
|
|
114
|
+
export async function loader() {
|
|
115
|
+
return undefined();
|
|
116
|
+
}
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
This will render the `instanceof Error` branch of the UI from step 1.
|
|
120
|
+
|
|
121
|
+
This is not just for loaders, but for all route module APIs: loaders, actions, components, headers, links, and meta.
|
|
122
|
+
|
|
123
|
+
## 3. Throw data in loaders/actions
|
|
124
|
+
|
|
125
|
+
[modes: framework,data]
|
|
126
|
+
|
|
127
|
+
There are exceptions to the rule in #2, especially 404s. You can intentionally `throw data()` (with a proper status code) to the closest error boundary when your loader can't find what it needs to render the page. Throw a 404 and move on.
|
|
128
|
+
|
|
129
|
+
```tsx
|
|
130
|
+
import { data } from "react-router";
|
|
131
|
+
|
|
132
|
+
export async function loader({ params }) {
|
|
133
|
+
let record = await fakeDb.getRecord(params.id);
|
|
134
|
+
if (!record) {
|
|
135
|
+
throw data("Record Not Found", { status: 404 });
|
|
136
|
+
}
|
|
137
|
+
return record;
|
|
138
|
+
}
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
This will render the `isRouteErrorResponse` branch of the UI from step 1.
|
|
142
|
+
|
|
143
|
+
## 4. Nested error boundaries
|
|
144
|
+
|
|
145
|
+
When an error is thrown, the "closest error boundary" will be rendered.
|
|
146
|
+
|
|
147
|
+
### Framework Mode
|
|
148
|
+
|
|
149
|
+
[modes: framework]
|
|
150
|
+
|
|
151
|
+
Consider these nested routes:
|
|
152
|
+
|
|
153
|
+
```tsx filename="routes.ts"
|
|
154
|
+
// ✅ has error boundary
|
|
155
|
+
route("/app", "app.tsx", [
|
|
156
|
+
// ❌ no error boundary
|
|
157
|
+
route("invoices", "invoices.tsx", [
|
|
158
|
+
// ✅ has error boundary
|
|
159
|
+
route("invoices/:id", "invoice-page.tsx", [
|
|
160
|
+
// ❌ no error boundary
|
|
161
|
+
route("payments", "payments.tsx"),
|
|
162
|
+
]),
|
|
163
|
+
]),
|
|
164
|
+
]);
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
The following table shows which error boundary will render given the origin of the error:
|
|
168
|
+
|
|
169
|
+
| error origin | rendered boundary |
|
|
170
|
+
| ---------------- | ----------------- |
|
|
171
|
+
| app.tsx | app.tsx |
|
|
172
|
+
| invoices.tsx | app.tsx |
|
|
173
|
+
| invoice-page.tsx | invoice-page.tsx |
|
|
174
|
+
| payments.tsx | invoice-page.tsx |
|
|
175
|
+
|
|
176
|
+
### Data Mode
|
|
177
|
+
|
|
178
|
+
[modes: data]
|
|
179
|
+
|
|
180
|
+
In Data Mode, the equivalent route tree might look like:
|
|
181
|
+
|
|
182
|
+
```tsx
|
|
183
|
+
let router = createBrowserRouter([
|
|
184
|
+
{
|
|
185
|
+
path: "/app",
|
|
186
|
+
Component: App,
|
|
187
|
+
ErrorBoundary: AppErrorBoundary, // ✅ has error boundary
|
|
188
|
+
children: [
|
|
189
|
+
{
|
|
190
|
+
path: "invoices",
|
|
191
|
+
Component: Invoices, // ❌ no error boundary
|
|
192
|
+
children: [
|
|
193
|
+
{
|
|
194
|
+
path: ":id",
|
|
195
|
+
Component: Invoice,
|
|
196
|
+
ErrorBoundary: InvoiceErrorBoundary, // ✅ has error boundary
|
|
197
|
+
children: [
|
|
198
|
+
{
|
|
199
|
+
path: "payments",
|
|
200
|
+
Component: Payments, // ❌ no error boundary
|
|
201
|
+
},
|
|
202
|
+
],
|
|
203
|
+
},
|
|
204
|
+
],
|
|
205
|
+
},
|
|
206
|
+
],
|
|
207
|
+
},
|
|
208
|
+
]);
|
|
209
|
+
```
|
|
210
|
+
|
|
211
|
+
The following table shows which error boundary will render given the origin of the error:
|
|
212
|
+
|
|
213
|
+
| error origin | rendered boundary |
|
|
214
|
+
| ------------ | ---------------------- |
|
|
215
|
+
| `App` | `AppErrorBoundary` |
|
|
216
|
+
| `Invoices` | `AppErrorBoundary` |
|
|
217
|
+
| `Invoice` | `InvoiceErrorBoundary` |
|
|
218
|
+
| `Payments` | `InvoiceErrorBoundary` |
|
|
219
|
+
|
|
220
|
+
## Error Sanitization
|
|
221
|
+
|
|
222
|
+
[modes: framework]
|
|
223
|
+
|
|
224
|
+
In Framework Mode when building for production, any errors that happen on the server are automatically sanitized before being sent to the browser to prevent leaking any sensitive server information (like stack traces).
|
|
225
|
+
|
|
226
|
+
This means that a thrown `Error` will have a generic message and no stack trace in production in the browser. The original error is untouched on the server.
|
|
227
|
+
|
|
228
|
+
Also note that data sent with `throw data(yourData)` is not sanitized as the data there is intended to be rendered.
|
|
229
|
+
|
|
230
|
+
[picking-a-mode]: ../start/modes
|
|
231
|
+
[type-safety]: ../explanation/type-safety
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Error Reporting
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
# Error Reporting
|
|
6
|
+
|
|
7
|
+
[MODES: framework,data]
|
|
8
|
+
|
|
9
|
+
<br/>
|
|
10
|
+
<br/>
|
|
11
|
+
|
|
12
|
+
React Router catches errors in your route modules and sends them to [error boundaries](./error-boundary) to prevent blank pages when errors occur. However, `ErrorBoundary` isn't sufficient for logging and reporting errors.
|
|
13
|
+
|
|
14
|
+
## Server Errors
|
|
15
|
+
|
|
16
|
+
[modes: framework]
|
|
17
|
+
|
|
18
|
+
To access these caught errors on the server, use the `handleError` export of the server entry module.
|
|
19
|
+
|
|
20
|
+
### 1. Reveal the server entry
|
|
21
|
+
|
|
22
|
+
If you don't see [`entry.server.tsx`][entryserver] in your app directory, you're using a default entry. Reveal it with this cli command:
|
|
23
|
+
|
|
24
|
+
```shellscript nonumber
|
|
25
|
+
react-router reveal entry.server
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
### 2. Export your error handler
|
|
29
|
+
|
|
30
|
+
This function is called whenever React Router catches an error in your application on the server.
|
|
31
|
+
|
|
32
|
+
```tsx filename=entry.server.tsx
|
|
33
|
+
import { type HandleErrorFunction } from "react-router";
|
|
34
|
+
|
|
35
|
+
export const handleError: HandleErrorFunction = (
|
|
36
|
+
error,
|
|
37
|
+
{ request },
|
|
38
|
+
) => {
|
|
39
|
+
// React Router may abort some interrupted requests, don't log those
|
|
40
|
+
if (!request.signal.aborted) {
|
|
41
|
+
myReportError(error);
|
|
42
|
+
|
|
43
|
+
// make sure to still log the error so you can see it
|
|
44
|
+
console.error(error);
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
See also:
|
|
50
|
+
|
|
51
|
+
- [`handleError`][handleError]
|
|
52
|
+
|
|
53
|
+
## Client Errors
|
|
54
|
+
|
|
55
|
+
To access these caught errors on the client, use the `onError` prop on your [`HydratedRouter`][hydratedrouter] or [`RouterProvider`][routerprovider] component.
|
|
56
|
+
|
|
57
|
+
### Framework Mode
|
|
58
|
+
|
|
59
|
+
[modes: framework]
|
|
60
|
+
|
|
61
|
+
#### 1. Reveal the client entry
|
|
62
|
+
|
|
63
|
+
If you don't see [`entry.client.tsx`][entryclient] in your app directory, you're using a default entry. Reveal it with this cli command:
|
|
64
|
+
|
|
65
|
+
```shellscript nonumber
|
|
66
|
+
react-router reveal entry.client
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
#### 2. Add your error handler
|
|
70
|
+
|
|
71
|
+
This function is called whenever React Router catches an error in your application on the client.
|
|
72
|
+
|
|
73
|
+
```tsx filename=entry.client.tsx
|
|
74
|
+
import { type ClientOnErrorFunction } from "react-router";
|
|
75
|
+
|
|
76
|
+
const onError: ClientOnErrorFunction = (
|
|
77
|
+
error,
|
|
78
|
+
{ location, params, pattern, errorInfo },
|
|
79
|
+
) => {
|
|
80
|
+
myReportError(error, location, errorInfo);
|
|
81
|
+
|
|
82
|
+
// make sure to still log the error so you can see it
|
|
83
|
+
console.error(error, errorInfo);
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
startTransition(() => {
|
|
87
|
+
hydrateRoot(
|
|
88
|
+
document,
|
|
89
|
+
<StrictMode>
|
|
90
|
+
<HydratedRouter onError={onError} />
|
|
91
|
+
</StrictMode>,
|
|
92
|
+
);
|
|
93
|
+
});
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
See also:
|
|
97
|
+
|
|
98
|
+
- [`<HydratedRouter onError>`][hydratedrouter-onerror]
|
|
99
|
+
|
|
100
|
+
### Data Mode
|
|
101
|
+
|
|
102
|
+
[modes: data]
|
|
103
|
+
|
|
104
|
+
This function is called whenever React Router catches an error in your application on the client.
|
|
105
|
+
|
|
106
|
+
```tsx
|
|
107
|
+
import {
|
|
108
|
+
createBrowserRouter,
|
|
109
|
+
type ClientOnErrorFunction,
|
|
110
|
+
} from "react-router";
|
|
111
|
+
import { RouterProvider } from "react-router/dom";
|
|
112
|
+
|
|
113
|
+
const onError: ClientOnErrorFunction = (
|
|
114
|
+
error,
|
|
115
|
+
{ location, params, pattern, errorInfo },
|
|
116
|
+
) => {
|
|
117
|
+
myReportError(error, location, errorInfo);
|
|
118
|
+
|
|
119
|
+
// make sure to still log the error so you can see it
|
|
120
|
+
console.error(error, errorInfo);
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
const router = createBrowserRouter(routes);
|
|
124
|
+
|
|
125
|
+
function App() {
|
|
126
|
+
return (
|
|
127
|
+
<RouterProvider router={router} onError={onError} />
|
|
128
|
+
);
|
|
129
|
+
}
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
See also:
|
|
133
|
+
|
|
134
|
+
- [`<RouterProvider onError>`][routerprovider-onerror]
|
|
135
|
+
|
|
136
|
+
[entryserver]: ../api/framework-conventions/entry.server.tsx
|
|
137
|
+
[handleError]: ../api/framework-conventions/entry.server.tsx#handleerror
|
|
138
|
+
[entryclient]: ../api/framework-conventions/entry.client.tsx
|
|
139
|
+
[hydratedrouter]: ../api/framework-routers/HydratedRouter
|
|
140
|
+
[routerprovider]: ../api/data-routers/RouterProvider
|
|
141
|
+
[hydratedrouter-onerror]: ../api/framework-routers/HydratedRouter#onError
|
|
142
|
+
[routerprovider-onerror]: ../api/data-routers/RouterProvider#onError
|
|
@@ -0,0 +1,307 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Using Fetchers
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
# Using Fetchers
|
|
6
|
+
|
|
7
|
+
[MODES: framework, data]
|
|
8
|
+
|
|
9
|
+
<br/>
|
|
10
|
+
<br/>
|
|
11
|
+
|
|
12
|
+
Fetchers are useful for creating complex, dynamic user interfaces that require multiple, concurrent data interactions without causing a navigation.
|
|
13
|
+
|
|
14
|
+
Fetchers track their own, independent state and can be used to load data, mutate data, submit forms, and generally interact with loaders and actions.
|
|
15
|
+
|
|
16
|
+
## Calling Actions
|
|
17
|
+
|
|
18
|
+
The most common case for a fetcher is to submit data to an action, triggering a revalidation of route data. Consider the following route module:
|
|
19
|
+
|
|
20
|
+
```tsx
|
|
21
|
+
import { useLoaderData } from "react-router";
|
|
22
|
+
|
|
23
|
+
export async function clientLoader({ request }) {
|
|
24
|
+
let title = localStorage.getItem("title") || "No Title";
|
|
25
|
+
return { title };
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export default function Component() {
|
|
29
|
+
let data = useLoaderData();
|
|
30
|
+
return (
|
|
31
|
+
<div>
|
|
32
|
+
<h1>{data.title}</h1>
|
|
33
|
+
</div>
|
|
34
|
+
);
|
|
35
|
+
}
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
### 1. Add an action
|
|
39
|
+
|
|
40
|
+
First we'll add an action to the route for the fetcher to call:
|
|
41
|
+
|
|
42
|
+
```tsx lines=[7-11]
|
|
43
|
+
import { useLoaderData } from "react-router";
|
|
44
|
+
|
|
45
|
+
export async function clientLoader({ request }) {
|
|
46
|
+
// ...
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export async function clientAction({ request }) {
|
|
50
|
+
await new Promise((res) => setTimeout(res, 1000));
|
|
51
|
+
let data = await request.formData();
|
|
52
|
+
localStorage.setItem("title", data.get("title"));
|
|
53
|
+
return { ok: true };
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export default function Component() {
|
|
57
|
+
let data = useLoaderData();
|
|
58
|
+
// ...
|
|
59
|
+
}
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
### 2. Create a fetcher
|
|
63
|
+
|
|
64
|
+
Next create a fetcher and render a form with it:
|
|
65
|
+
|
|
66
|
+
```tsx lines=[7,12-14]
|
|
67
|
+
import { useLoaderData, useFetcher } from "react-router";
|
|
68
|
+
|
|
69
|
+
// ...
|
|
70
|
+
|
|
71
|
+
export default function Component() {
|
|
72
|
+
let data = useLoaderData();
|
|
73
|
+
let fetcher = useFetcher();
|
|
74
|
+
return (
|
|
75
|
+
<div>
|
|
76
|
+
<h1>{data.title}</h1>
|
|
77
|
+
|
|
78
|
+
<fetcher.Form method="post">
|
|
79
|
+
<input type="text" name="title" />
|
|
80
|
+
</fetcher.Form>
|
|
81
|
+
</div>
|
|
82
|
+
);
|
|
83
|
+
}
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
### 3. Submit the form
|
|
87
|
+
|
|
88
|
+
If you submit the form now, the fetcher will call the action and revalidate the route data automatically.
|
|
89
|
+
|
|
90
|
+
### 4. Render pending state
|
|
91
|
+
|
|
92
|
+
Fetchers make their state available during the async work so you can render pending UI the moment the user interacts:
|
|
93
|
+
|
|
94
|
+
```tsx lines=[10]
|
|
95
|
+
export default function Component() {
|
|
96
|
+
let data = useLoaderData();
|
|
97
|
+
let fetcher = useFetcher();
|
|
98
|
+
return (
|
|
99
|
+
<div>
|
|
100
|
+
<h1>{data.title}</h1>
|
|
101
|
+
|
|
102
|
+
<fetcher.Form method="post">
|
|
103
|
+
<input type="text" name="title" />
|
|
104
|
+
{fetcher.state !== "idle" && <p>Saving...</p>}
|
|
105
|
+
</fetcher.Form>
|
|
106
|
+
</div>
|
|
107
|
+
);
|
|
108
|
+
}
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
### 5. Optimistic UI
|
|
112
|
+
|
|
113
|
+
Sometimes there's enough information in the form to render the next state immediately. You can access the form data with `fetcher.formData`:
|
|
114
|
+
|
|
115
|
+
```tsx lines=[3-4,8]
|
|
116
|
+
export default function Component() {
|
|
117
|
+
let data = useLoaderData();
|
|
118
|
+
let fetcher = useFetcher();
|
|
119
|
+
let title = fetcher.formData?.get("title") || data.title;
|
|
120
|
+
|
|
121
|
+
return (
|
|
122
|
+
<div>
|
|
123
|
+
<h1>{title}</h1>
|
|
124
|
+
|
|
125
|
+
<fetcher.Form method="post">
|
|
126
|
+
<input type="text" name="title" />
|
|
127
|
+
{fetcher.state !== "idle" && <p>Saving...</p>}
|
|
128
|
+
</fetcher.Form>
|
|
129
|
+
</div>
|
|
130
|
+
);
|
|
131
|
+
}
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
### 6. Fetcher Data and Validation
|
|
135
|
+
|
|
136
|
+
Data returned from an action is available in the fetcher's `data` property. This is primarily useful for returning error messages to the user for a failed mutation:
|
|
137
|
+
|
|
138
|
+
```tsx lines=[7-10,28-32]
|
|
139
|
+
// ...
|
|
140
|
+
|
|
141
|
+
export async function clientAction({ request }) {
|
|
142
|
+
await new Promise((res) => setTimeout(res, 1000));
|
|
143
|
+
let data = await request.formData();
|
|
144
|
+
|
|
145
|
+
let title = data.get("title") as string;
|
|
146
|
+
if (title.trim() === "") {
|
|
147
|
+
return { ok: false, error: "Title cannot be empty" };
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
localStorage.setItem("title", title);
|
|
151
|
+
return { ok: true, error: null };
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
export default function Component() {
|
|
155
|
+
let data = useLoaderData();
|
|
156
|
+
let fetcher = useFetcher();
|
|
157
|
+
let title = fetcher.formData?.get("title") || data.title;
|
|
158
|
+
|
|
159
|
+
return (
|
|
160
|
+
<div>
|
|
161
|
+
<h1>{title}</h1>
|
|
162
|
+
|
|
163
|
+
<fetcher.Form method="post">
|
|
164
|
+
<input type="text" name="title" />
|
|
165
|
+
{fetcher.state !== "idle" && <p>Saving...</p>}
|
|
166
|
+
{fetcher.data?.error && (
|
|
167
|
+
<p style={{ color: "red" }}>
|
|
168
|
+
{fetcher.data.error}
|
|
169
|
+
</p>
|
|
170
|
+
)}
|
|
171
|
+
</fetcher.Form>
|
|
172
|
+
</div>
|
|
173
|
+
);
|
|
174
|
+
}
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
## Loading Data
|
|
178
|
+
|
|
179
|
+
Another common use case for fetchers is to load data from a route for something like a combobox.
|
|
180
|
+
|
|
181
|
+
### 1. Create a search route
|
|
182
|
+
|
|
183
|
+
Consider the following route with a very basic search:
|
|
184
|
+
|
|
185
|
+
```tsx filename=./search-users.tsx
|
|
186
|
+
// { path: '/search-users', filename: './search-users.tsx' }
|
|
187
|
+
const users = [
|
|
188
|
+
{ id: 1, name: "Ryan" },
|
|
189
|
+
{ id: 2, name: "Michael" },
|
|
190
|
+
// ...
|
|
191
|
+
];
|
|
192
|
+
|
|
193
|
+
export async function loader({ request }) {
|
|
194
|
+
await new Promise((res) => setTimeout(res, 300));
|
|
195
|
+
let url = new URL(request.url);
|
|
196
|
+
let query = url.searchParams.get("q");
|
|
197
|
+
return users.filter((user) =>
|
|
198
|
+
user.name.toLowerCase().includes(query.toLowerCase()),
|
|
199
|
+
);
|
|
200
|
+
}
|
|
201
|
+
```
|
|
202
|
+
|
|
203
|
+
### 2. Render a fetcher in a combobox component
|
|
204
|
+
|
|
205
|
+
```tsx
|
|
206
|
+
import { useFetcher } from "react-router";
|
|
207
|
+
|
|
208
|
+
export function UserSearchCombobox() {
|
|
209
|
+
let fetcher = useFetcher();
|
|
210
|
+
return (
|
|
211
|
+
<div>
|
|
212
|
+
<fetcher.Form method="get" action="/search-users">
|
|
213
|
+
<input type="text" name="q" />
|
|
214
|
+
</fetcher.Form>
|
|
215
|
+
</div>
|
|
216
|
+
);
|
|
217
|
+
}
|
|
218
|
+
```
|
|
219
|
+
|
|
220
|
+
- The action points to the route we created above: "/search-users"
|
|
221
|
+
- The name of the input is "q" to match the query parameter
|
|
222
|
+
|
|
223
|
+
### 3. Add type inference
|
|
224
|
+
|
|
225
|
+
```tsx lines=[2,5]
|
|
226
|
+
import { useFetcher } from "react-router";
|
|
227
|
+
import type { loader } from "./search-users";
|
|
228
|
+
|
|
229
|
+
export function UserSearchCombobox() {
|
|
230
|
+
let fetcher = useFetcher<typeof loader>();
|
|
231
|
+
// ...
|
|
232
|
+
}
|
|
233
|
+
```
|
|
234
|
+
|
|
235
|
+
Ensure you use `import type` so you only import the types.
|
|
236
|
+
|
|
237
|
+
### 4. Render the data
|
|
238
|
+
|
|
239
|
+
```tsx lines=[10-16]
|
|
240
|
+
import { useFetcher } from "react-router";
|
|
241
|
+
|
|
242
|
+
export function UserSearchCombobox() {
|
|
243
|
+
let fetcher = useFetcher<typeof loader>();
|
|
244
|
+
return (
|
|
245
|
+
<div>
|
|
246
|
+
<fetcher.Form method="get" action="/search-users">
|
|
247
|
+
<input type="text" name="q" />
|
|
248
|
+
</fetcher.Form>
|
|
249
|
+
{fetcher.data && (
|
|
250
|
+
<ul>
|
|
251
|
+
{fetcher.data.map((user) => (
|
|
252
|
+
<li key={user.id}>{user.name}</li>
|
|
253
|
+
))}
|
|
254
|
+
</ul>
|
|
255
|
+
)}
|
|
256
|
+
</div>
|
|
257
|
+
);
|
|
258
|
+
}
|
|
259
|
+
```
|
|
260
|
+
|
|
261
|
+
Note you will need to hit "enter" to submit the form and see the results.
|
|
262
|
+
|
|
263
|
+
### 5. Render a pending state
|
|
264
|
+
|
|
265
|
+
```tsx lines=[12-14]
|
|
266
|
+
import { useFetcher } from "react-router";
|
|
267
|
+
|
|
268
|
+
export function UserSearchCombobox() {
|
|
269
|
+
let fetcher = useFetcher<typeof loader>();
|
|
270
|
+
return (
|
|
271
|
+
<div>
|
|
272
|
+
<fetcher.Form method="get" action="/search-users">
|
|
273
|
+
<input type="text" name="q" />
|
|
274
|
+
</fetcher.Form>
|
|
275
|
+
{fetcher.data && (
|
|
276
|
+
<ul
|
|
277
|
+
style={{
|
|
278
|
+
opacity: fetcher.state === "idle" ? 1 : 0.25,
|
|
279
|
+
}}
|
|
280
|
+
>
|
|
281
|
+
{fetcher.data.map((user) => (
|
|
282
|
+
<li key={user.id}>{user.name}</li>
|
|
283
|
+
))}
|
|
284
|
+
</ul>
|
|
285
|
+
)}
|
|
286
|
+
</div>
|
|
287
|
+
);
|
|
288
|
+
}
|
|
289
|
+
```
|
|
290
|
+
|
|
291
|
+
### 6. Search on user input
|
|
292
|
+
|
|
293
|
+
Fetchers can be submitted programmatically with `fetcher.submit`:
|
|
294
|
+
|
|
295
|
+
```tsx lines=[5-7]
|
|
296
|
+
<fetcher.Form method="get" action="/search-users">
|
|
297
|
+
<input
|
|
298
|
+
type="text"
|
|
299
|
+
name="q"
|
|
300
|
+
onChange={(event) => {
|
|
301
|
+
fetcher.submit(event.currentTarget.form);
|
|
302
|
+
}}
|
|
303
|
+
/>
|
|
304
|
+
</fetcher.Form>
|
|
305
|
+
```
|
|
306
|
+
|
|
307
|
+
Note the input event's form is passed as the first argument to `fetcher.submit`. The fetcher will use that form to submit the request, reading its attributes and serializing the data from its elements.
|