Skip to content

Commit 0a7a539

Browse files
committed
Present Activity pre-rendering step by step
1 parent 3d3b7c2 commit 0a7a539

1 file changed

Lines changed: 91 additions & 1 deletion

File tree

‎src/content/reference/react/Activity.md‎

Lines changed: 91 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -521,7 +521,97 @@ An Activity boundary can also prepare content before the user sees it. Content i
521521

522522
If `Posts` suspends while reading code or data, React continues rendering the rest of the page while the hidden work proceeds.
523523

524-
The following example renders both tabs inside Activity boundaries. `<Posts>` reads a cached Promise with [`use`](/reference/react/use), so React begins loading the posts while its boundary is hidden. Wait briefly before selecting **Posts**. If the hidden render has finished, the list appears immediately.
524+
The Posts tab in the following example reads a cached Promise with [`use`](/reference/react/use). Select **Posts** to see the Suspense fallback while the data loads. Because conditional rendering does not mount `<Posts>` until its tab is active, it cannot begin loading the posts ahead of time.
525+
526+
<Sandpack>
527+
528+
```js src/App.js active
529+
import { Suspense, useState } from 'react';
530+
import Home from './Home.js';
531+
import Posts from './Posts.js';
532+
533+
export default function App() {
534+
const [activeTab, setActiveTab] = useState('home');
535+
536+
return (
537+
<>
538+
<div aria-label="Profile sections" role="group">
539+
<button
540+
aria-pressed={activeTab === 'home'}
541+
onClick={() => setActiveTab('home')}
542+
>
543+
Home
544+
</button>
545+
<button
546+
aria-pressed={activeTab === 'posts'}
547+
onClick={() => setActiveTab('posts')}
548+
>
549+
Posts
550+
</button>
551+
</div>
552+
553+
<Suspense fallback={<h1>Loading posts...</h1>}>
554+
{activeTab === 'home' && <Home />}
555+
{activeTab === 'posts' && <Posts />}
556+
</Suspense>
557+
</>
558+
);
559+
}
560+
```
561+
562+
```js src/Posts.js
563+
import { use } from 'react';
564+
import { getPosts } from './data.js';
565+
566+
export default function Posts() {
567+
const posts = use(getPosts());
568+
569+
return (
570+
<ul>
571+
{posts.map(post => (
572+
<li key={post.id}>{post.title}</li>
573+
))}
574+
</ul>
575+
);
576+
}
577+
```
578+
579+
```js src/Home.js hidden
580+
export default function Home() {
581+
return <p>Welcome to my profile!</p>;
582+
}
583+
```
584+
585+
```js src/data.js hidden
586+
let postsPromise;
587+
588+
export function getPosts() {
589+
if (!postsPromise) {
590+
postsPromise = loadPosts();
591+
}
592+
return postsPromise;
593+
}
594+
595+
async function loadPosts() {
596+
// Add a delay so that loading is easier to observe.
597+
await new Promise(resolve => setTimeout(resolve, 1500));
598+
599+
return Array.from({ length: 5 }, (_, index) => ({
600+
id: index,
601+
title: 'Post #' + (index + 1),
602+
}));
603+
}
604+
```
605+
606+
```css
607+
button {
608+
margin-right: 8px;
609+
}
610+
```
611+
612+
</Sandpack>
613+
614+
To start loading the posts before the user selects the tab, render both tabs inside Activity boundaries. React begins rendering `<Posts>` while its boundary is hidden. Wait briefly before selecting **Posts**. If the hidden render has finished, the list appears immediately.
525615

526616
<Sandpack>
527617

0 commit comments

Comments
 (0)