Skip to content

Commit e45ac55

Browse files
authored
Better use(Promise) example in 19 blog (#6790)
* Better use(Promise) example in 19 blog * Better use(Promise) example in 19 blog
1 parent e09ff1c commit e45ac55

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

src/content/blog/2024/04/25/react-19.md

+13-4
Original file line numberDiff line numberDiff line change
@@ -243,14 +243,23 @@ In React 19 we're introducing a new API to read resources in render: `use`.
243243

244244
For example, you can read a promise with `use`, and React will Suspend until the promise resolves:
245245

246-
```js {1,6}
246+
```js {1,5}
247247
import {use} from 'react';
248248

249249
function Comments({commentsPromise}) {
250-
// NOTE: this will resume the promise from the server.
251-
// It will suspend until the data is available.
250+
// `use` will suspend until the promise resolves.
252251
const comments = use(commentsPromise);
253-
return comments.map(comment => <p>{comment}</p>);
252+
return comments.map(comment => <p key={comment.id}>{comment}</p>);
253+
}
254+
255+
function Page({commentsPromise}) {
256+
// When `use` suspends in Comments,
257+
// this Suspense boundary will be shown.
258+
return (
259+
<Suspense fallback={<div>Loading...</div>}>
260+
<Comments commentsPromise={commentsPromise} />
261+
</Suspense>
262+
)
254263
}
255264
```
256265

0 commit comments

Comments
 (0)