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
Returning a `deferred` from a `loader` allows you to separate _critical_ loader data that you want to wait for prior to rendering the destination page from _non-critical_ data that you are OK to show a spinner for until it loads.
9
+
10
+
```jsx
11
+
// In your route loader, return a deferred() and choose per-key whether to
12
+
// await the promise or not. As soon as the awaited promises resolve, the
13
+
// page will be rendered.
14
+
functionloader() {
15
+
returndeferred({
16
+
critical:awaitgetCriticalData(),
17
+
lazy1:getLazyData(),
18
+
});
19
+
};
20
+
21
+
// In your route element, grab the values from useLoaderData and render them
22
+
// with <Deferred>
23
+
functionDeferredPage() {
24
+
let data =useLoaderData();
25
+
return (
26
+
<>
27
+
<p>Critical Data: {data.critical}</p>
28
+
<Deferred
29
+
value={data.lazy}
30
+
fallback={<p>Loading...</p>}
31
+
errorElement={<RenderDeferredError />}>
32
+
<RenderDeferredData />
33
+
</Deferred>
34
+
</>
35
+
);
36
+
}
37
+
38
+
// Use separate components to render the data once it resolves, and access it
39
+
// via the useDeferredData hook
40
+
functionRenderDeferredData() {
41
+
let data =useDeferredData();
42
+
return<p>Lazy: {data}</p>;
43
+
}
44
+
45
+
functionRenderDeferredError() {
46
+
let data =useRouteError();
47
+
return<p>Error! {data.message} {data.stack}</p>;
48
+
}
49
+
```
50
+
51
+
If you want to skip the separate components, you can use the Render Props
52
+
pattern and handle the rendering of the deferred data inline:
0 commit comments