You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/route/lazy.md
+66Lines changed: 66 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -59,4 +59,70 @@ export function ErrorBoundary() {
59
59
ErrorBoundary.displayName="SampleErrorBoundary";
60
60
```
61
61
62
+
## Statically Defined Properties
63
+
64
+
Any properties defined statically on the route cannot be overwritten by the `lazy` function, and you'll receive a console warning if you attempt to overwrite them.
65
+
66
+
Additionally, as an optimization, if you statically define a `loader`/`action` then it will be called in parallel with the `lazy` function. This is useful if you have slim loaders that you don't mind on the critical bundle, and would like to kick off their data fetches in parallel with the component download. This is close to how Remix handles fetching because each route is it's own API route.
This also allows you to do more granular code splitting. For example, you could split your `loader` and `Component` into different files for parallel downloading:
77
+
78
+
```js
79
+
let route = {
80
+
path:"projects",
81
+
asyncloader({ request, params }) {
82
+
let { loader } =awaitimport("./projects-loader");
83
+
returnloader({ request, params });
84
+
},
85
+
lazy: () =>import("./projects-component"),
86
+
};
87
+
```
88
+
89
+
## Multiple Routes in a single file
90
+
91
+
While `lazy` may generally be used 1:1 with an async `import()` per route, you are free to implement a more advanced `lazy` function and just need to return the properties you want added to that route. This opens up some interesting possibilities.
92
+
93
+
For example, if you want to avoid loading multiple chunks for nested routes, you could store them all in the same file and return them to the individual routes. Modern bundlers will latch onto the same Promise for the different `import()` invocations.
94
+
95
+
```js
96
+
// Assume pages/Dashboard.jsx has all of our loaders/components for multiple
Copy file name to clipboardExpand all lines: docs/route/route.md
+28-4Lines changed: 28 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -296,17 +296,27 @@ The route action is called when a submission is sent to the route from a [Form][
296
296
297
297
Please see the [action][action] documentation for more details.
298
298
299
-
## `element`
299
+
## `element`/`Component`
300
300
301
-
The element to render when the route matches the URL.
301
+
The React Element/Component to render when the route matches the URL.
302
+
303
+
If you want to create the React Element, use `element`:
302
304
303
305
```tsx
304
306
<Routepath="/for-sale"element={<Properties />} />
305
307
```
306
308
307
-
## `errorElement`
309
+
Otherwise use `Component` and React Router will create the React Element for you:
310
+
311
+
```tsx
312
+
<Routepath="/for-sale"Component={Properties} />
313
+
```
314
+
315
+
## `errorElement`/`ErrorBoundary`
308
316
309
-
When a route throws an exception while rendering, in a `loader` or in an `action`, this element will render instead of the normal `element`.
317
+
When a route throws an exception while rendering, in a `loader` or in an `action`, this React Element/Component will render instead of the normal `element`/`Component`.
318
+
319
+
If you want to create the React Element on your own, use `errorElement`:
310
320
311
321
```tsx
312
322
<Route
@@ -324,6 +334,20 @@ When a route throws an exception while rendering, in a `loader` or in an `action
324
334
/>
325
335
```
326
336
337
+
Otherwise use `ErrorBoundary` and React Router will create the React Element for you:
338
+
339
+
```tsx
340
+
<Route
341
+
path="/for-sale"
342
+
Component={Properties}
343
+
loader={() =>loadProperties()}
344
+
action={async ({ request }) =>
345
+
createProperty(awaitrequest.formData())
346
+
}
347
+
ErrorBoundary={ErrorBoundary}
348
+
/>
349
+
```
350
+
327
351
<docs-warning>If you are not using a data router like [`createBrowserRouter`][createbrowserrouter], this will do nothing</docs-warning>
328
352
329
353
Please see the [errorElement][errorelement] documentation for more details.
0 commit comments