Skip to content

Commit a3c5c70

Browse files
committed
Refine based on feedback
1 parent af59d7e commit a3c5c70

1 file changed

Lines changed: 22 additions & 11 deletions

File tree

  • src/content/reference/react

src/content/reference/react/use.md

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1113,47 +1113,58 @@ root.render(
11131113
11141114
#### Should I resolve a Promise in a Server or Client Component? {/*resolve-promise-in-server-or-client-component*/}
11151115
1116-
A Promise can be resolved with `await` in a Server Component, or passed as a prop to a Client Component and resolved there with `use`.
1116+
If you have a Promise, at some point you need to unwrap it to read its value. You unwrap it with `await` on the server, and with `use` on the client.
11171117
1118-
Using `await` in a Server Component suspends the Server Component itself, and the Client Component receives the resolved value as a prop:
1118+
Usually, the simplest option is to `await` the Promise where you create it. The Server Component suspends until the data is ready, and everything below it waits too:
11191119
11201120
```js
11211121
// Server Component
11221122
export default async function App() {
1123-
// Will suspend the Server Component.
11241123
const messageContent = await fetchMessage();
11251124
return <Message messageContent={messageContent} />;
11261125
}
11271126
```
11281127
1129-
A Server Component can also start a Promise without awaiting it and pass the Promise to a Client Component. The Server Component returns immediately, and the Client Component suspends when it calls `use`:
1128+
However, you don't have to unwrap it right away. You can pass the Promise down as a prop, and unwrap it deeper in the tree. The component that reads the Promise still suspends, but only that part of the tree waits for the data. Wrap that component in a [`<Suspense>`](/reference/react/Suspense) boundary to show a fallback while the rest of the page renders immediately.
1129+
1130+
For example, a deeper Server Component can `await` the Promise it receives:
11301131
11311132
```js
1133+
import { Suspense } from 'react';
1134+
11321135
// Server Component
11331136
export default function App() {
1134-
// Not awaited: starts on the server, streamed to the client.
11351137
const messagePromise = fetchMessage();
1136-
return <Message messagePromise={messagePromise} />;
1138+
return (
1139+
<Suspense fallback={<p>⌛Downloading message...</p>}>
1140+
<Message messagePromise={messagePromise} />
1141+
</Suspense>
1142+
);
1143+
}
1144+
1145+
// Server Component
1146+
async function Message({ messagePromise }) {
1147+
const messageContent = await messagePromise;
1148+
return <p>{messageContent}</p>;
11371149
}
11381150
```
11391151
1152+
Or, in a separate file, a Client Component can unwrap the same Promise with `use`:
1153+
11401154
```js
11411155
// Client Component
11421156
'use client';
11431157
import { use } from 'react';
11441158

11451159
export function Message({ messagePromise }) {
1146-
// Will suspend until the data is available.
11471160
const messageContent = use(messagePromise);
11481161
return <p>{messageContent}</p>;
11491162
}
11501163
```
11511164
1152-
Prefer `await` in a Server Component when possible. 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`.
1153-
1154-
Pass a Promise to a Client Component to suspend deeper in the tree, letting more of the surrounding UI render while the Promise is pending. A common case is interactive content like popovers and tooltips, where the data is only needed after a hover or click. Client Components can't `await`, so they read a Promise with `use`.
1165+
Passing the Promise down works the same way in both cases. Both suspend where the Promise is read, and both unblock the UI above. The only difference is that Client Components can't `await` during render, so they unwrap the Promise with `use` instead. A common case is interactive content like popovers and tooltips, where the data is only needed after a hover or click.
11551166
1156-
In either case, wrap the component that reads the Promise in a Suspense boundary so React can show a fallback while the Promise is pending. See [Revealing content together at once](/reference/react/Suspense#revealing-content-together-at-once) for guidance on boundary placement.
1167+
See [Revealing content together at once](/reference/react/Suspense#revealing-content-together-at-once) for guidance on where to place Suspense boundaries.
11571168
11581169
</DeepDive>
11591170

0 commit comments

Comments
 (0)