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
{{ message }}
Repository navigation
Commit c46d999
Browse filesBrowse the repository at this point in the historyBrowse files
Copy file name to clipboardExpand all lines: src/content/reference/react/use.md
+26-17Lines changed: 26 additions & 17 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -49,7 +49,7 @@ The context value for the passed context, determined by the closest context prov
49
49
50
50
### `use(promise)` {/*use-promise*/}
51
51
52
-
Call `use` with a [Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) to read its resolved value. The component calling `use` *suspends* while the Promise is pending.
52
+
Call `use` with a [Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) to read its resolved value. The component calling `use` *suspends* while the Promise is pending. Despite its name, `use` is not a Hook. Unlike Hooks, it can be called inside loops and conditional statements like `if`.
53
53
54
54
```js
55
55
import { use } from'react';
@@ -59,7 +59,7 @@ function MessageComponent({ messagePromise }) {
59
59
// ...
60
60
```
61
61
62
-
If the component that calls `use` is wrapped in a [`Suspense`](/reference/react/Suspense) boundary, the fallback will be displayed while the Promise is pending. Once the Promise is resolved, the Suspense fallback is replaced by the rendered components using the data returned by `use`. If the Promise is rejected, the fallback of the nearest [Error Boundary](/reference/react/Component#catching-rendering-errors-with-an-error-boundary) will be displayed.
62
+
If the component that calls `use` is wrapped in a [Suspense](/reference/react/Suspense) boundary, the fallback will be displayed while the Promise is pending. Once the Promise is resolved, the Suspense fallback is replaced by the rendered components using the data returned by `use`. If the Promise is rejected, the fallback of the nearest [Error Boundary](/reference/react/Component#catching-rendering-errors-with-an-error-boundary) will be displayed.
63
63
64
64
[See more examples below.](#usage-promises)
65
65
@@ -74,10 +74,9 @@ The resolved value of the Promise.
74
74
#### Caveats {/*promise-caveats*/}
75
75
76
76
* `use` must be called inside a Component or a Hook.
77
-
* Despite its name, `use` is not a Hook. Unlike Hooks, it can be called inside loops and conditional statements like `if`.
78
77
* `use` cannot be called inside a try-catch block. Instead, wrap your component in an [Error Boundary](#displaying-an-error-with-an-error-boundary) to catch the error and display a fallback.
79
78
* Promises passed to `use` must be cached so the same Promise instance is reused across re-renders. [See caching Promises below.](#caching-promises-for-client-components)
80
-
* When passing a Promise from a Server Component to a Client Component, its resolved value must be [serializable](/reference/rsc/use-client#serializable-types). Data types like functions aren't serializable and cannot be the resolved value of such a Promise.
79
+
* When passing a Promise from a Server Component to a Client Component, its resolved value must be [serializable](/reference/rsc/use-client#serializable-types).
81
80
82
81
---
83
82
@@ -224,7 +223,7 @@ function Button({ show, children }) {
224
223
225
224
### Reading a Promise from context {/*reading-a-promise-from-context*/}
226
225
227
-
You can pass a Promise through context to share data without prop drilling. Pass the Promise as the context value, then call `use(context)`to read the Promise and `use(promise)` to read its resolved value:
226
+
You can pass a Promise through context to share data without prop drilling. Pass the Promise as the context value, then read it with `use(context)`and resolve it with `use(promise)`:
228
227
229
228
```js
230
229
import { use } from'react';
@@ -237,11 +236,11 @@ function Profile() {
237
236
}
238
237
```
239
238
240
-
Wrap the components that read the Promise in [`<Suspense>`](/reference/react/Suspense) so only that subtree suspends while the Promise is pending. See [Usage (Promises)](#usage-promises) below for more on reading Promises with `use`.
239
+
Wrap the components that read the Promise in a [Suspense](/reference/react/Suspense) boundary so only that subtree suspends while the Promise is pending. See [Usage (Promises)](#usage-promises) below for more on reading Promises with `use`.
241
240
242
241
<Pitfall>
243
242
244
-
Passing a Promise through context is convenient, but comes with trade-offs. Prefer passing the Promise as a [prop](#reading-a-promise-with-use) from a [Server Component](/reference/rsc/server-components) when possible.
243
+
Reading a Promise from context comes with trade-offs. Prefer passing the Promise as a [prop](#reading-a-promise-with-use) from a [Server Component](/reference/rsc/server-components) when possible.
245
244
246
245
* **Double unwrap.** Reading the value requires two calls: `use(context)` to get the Promise, then `use(promise)` to get its resolved value. The context value itself is not awaited.
247
246
* **Revalidation scope.** Because the Promise is created in the component that provides it, revalidating the data requires re-rendering that component. Hoisting data into a component high in the tree means everything below it re-renders to update the data.
@@ -274,7 +273,7 @@ function Albums({ albumsPromise }) {
274
273
}
275
274
```
276
275
277
-
The component that calls <CodeStep step={1}>`use`</CodeStep> must be wrapped in a [`Suspense`](/reference/react/Suspense) boundary. While the Promise is pending, the Suspense fallback is displayed. Once the Promise resolves, React reads the value with `use` and replaces the fallback with the rendered component.
276
+
Wrap the component that calls <CodeStep step={1}>`use`</CodeStep> in a [Suspense](/reference/react/Suspense) boundary so React can show a fallback while the Promise is pending. The closest Suspense boundary above the suspending component shows its fallback. Once the Promise resolves, React reads the value with `use` and replaces the fallback with the rendered component.
278
277
279
278
<Recipes titleText="Reading a Promise with use vs fetching in an Effect" titleId="examples-promise">
280
279
@@ -497,7 +496,7 @@ function Albums() {
497
496
// 🔴 `fetch` creates a new Promise on every render.
498
497
constalbums=use(fetch('/albums'));
499
498
500
-
// 🔴 Inline `async` function calls create a new Promise on every render.
499
+
// 🔴 Uncached `async` function calls create a new Promise on every render.
501
500
constalbums=use((async () => {
502
501
constres=awaitfetch('/albums');
503
502
returnres.json();
@@ -1040,7 +1039,7 @@ export function Message({ messagePromise }) {
1040
1039
return<p>Here is the message: {messageContent}</p>;
1041
1040
}
1042
1041
```
1043
-
Because `Message` is wrapped in [`Suspense`](/reference/react/Suspense), the fallback will be displayed until the Promise is resolved. When the Promise is resolved, the value will be read by the `use` API and the `Message` component will replace the Suspense fallback.
1042
+
Because `Message` is wrapped in a [Suspense](/reference/react/Suspense) boundary, the fallback will be displayed until the Promise is resolved. When the Promise is resolved, the value will be read by the `use` API and the `Message` component will replace the Suspense fallback.
1044
1043
1045
1044
<Sandpack>
1046
1045
@@ -1111,33 +1110,43 @@ root.render(
1111
1110
1112
1111
#### Should I resolve a Promise in a Server or Client Component? {/*resolve-promise-in-server-or-client-component*/}
1113
1112
1114
-
A Promise can be passed from a Server Component to a Client Component and resolved in the Client Component with the `use` API. You can also resolve the Promise in a Server Component with `await` and pass the resolved value to the Client Component as a prop. The difference comes down to how much of your UI you want to reveal before the data is ready.
1115
-
1116
-
Using `await` in a [Server Component](/reference/rsc/server-components) suspends the Server Component itself, so React won't continue rendering it until the Promise resolves:
1113
+
In general, prefer resolving the Promise with `await` in a Server Component. The Server Component suspends until the Promise resolves, and the Client Component receives the resolved value as a prop:
Passing the Promise to a Client Component doesn't suspend the Server Component. The Server Component returns immediately, and the Client Component suspends when it calls `use`:
1124
+
Resolve the Promise with `use` in a Client Component for data that isn't important for the surrounding UI to render, like below-the-fold content, or content revealed by a hover or click (popovers, tooltips). The Server Component starts the Promise but doesn't await it, so it returns immediately. The Client Component suspends when it calls `use`:
1127
1125
1128
1126
```js
1129
1127
// Server Component
1130
1128
exportdefaultfunctionApp() {
1129
+
// Not awaited: starts here, resolves on the client.
In both cases, the component that reads the Promise suspends. Make sure there's a [`<Suspense>`](/reference/react/Suspense) boundary above it so React can display a fallback there and keep rendering the rest of the page. Without a boundary above the suspending component, the suspension bubbles up the tree and blocks the surrounding UI until the Promise resolves.
1135
+
```js
1136
+
// Client Component
1137
+
'use client';
1138
+
import { use } from'react';
1139
+
1140
+
exportfunctionMessage({ messagePromise }) {
1141
+
// Will suspend until the data is available.
1142
+
constmessageContent=use(messagePromise);
1143
+
return<p>{messageContent}</p>;
1144
+
}
1145
+
```
1137
1146
1138
-
Passing the Promise down is useful when a Client Component needs the data and you want to reveal as much of the surrounding UI as possible while the Promise is pending. Because the Client Component is the component that suspends, you can place the `<Suspense>` boundary close to it and let the rest of the page render right away. See [Displaying a fallback while content is loading](/reference/react/Suspense#displaying-a-fallback-while-content-is-loading) for more on boundary placement.
1147
+
This way the Client Component suspends without blocking the rest of the page from rendering. If a Server Component above already awaits the data, pass the resolved value down as a prop instead of creating a new Promise to call `use`.
1139
1148
1140
-
When the surrounding UI is small or depends on the same data, prefer `await` on the server. If a Server Component above already awaits the data, pass the resolved value down instead of creating a new Promise to call `use`.
1149
+
In both cases, wrap the component that reads the Promise in a Suspense boundary so React can display a fallback while the Promise is pending.
0 commit comments