Skip to content

Commit e42b9a7

Browse files
committed
Simplify the Activity pre-rendering example
1 parent b119ed4 commit e42b9a7

1 file changed

Lines changed: 1 addition & 105 deletions

File tree

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

Lines changed: 1 addition & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -513,107 +513,7 @@ An Activity boundary can also prepare content before the user sees it. Content i
513513

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

516-
The following examples compare rendering the Posts tab on demand with pre-rendering it in a hidden Activity boundary. Both versions read the same cached Promise with [`use`](/reference/react/use).
517-
518-
<Recipes titleText="The difference between rendering and pre-rendering a tab" titleId="examples-pre-rendering">
519-
520-
#### Rendering the tab on demand shows a fallback {/*rendering-the-tab-on-demand-shows-a-fallback*/}
521-
522-
This version does not mount `<Posts>` until its tab is active. Select **Posts** to see the Suspense fallback while the data loads.
523-
524-
<Sandpack>
525-
526-
```js src/App.js active
527-
import { Suspense, useState } from 'react';
528-
import Home from './Home.js';
529-
import Posts from './Posts.js';
530-
531-
export default function App() {
532-
const [activeTab, setActiveTab] = useState('home');
533-
534-
return (
535-
<>
536-
<div aria-label="Profile sections" role="group">
537-
<button
538-
aria-pressed={activeTab === 'home'}
539-
onClick={() => setActiveTab('home')}
540-
>
541-
Home
542-
</button>
543-
<button
544-
aria-pressed={activeTab === 'posts'}
545-
onClick={() => setActiveTab('posts')}
546-
>
547-
Posts
548-
</button>
549-
</div>
550-
551-
<Suspense fallback={<h1>Loading posts...</h1>}>
552-
{activeTab === 'home' && <Home />}
553-
{activeTab === 'posts' && <Posts />}
554-
</Suspense>
555-
</>
556-
);
557-
}
558-
```
559-
560-
```js src/Posts.js
561-
import { use } from 'react';
562-
import { getPosts } from './data.js';
563-
564-
export default function Posts() {
565-
const posts = use(getPosts());
566-
567-
return (
568-
<ul>
569-
{posts.map(post => (
570-
<li key={post.id}>{post.title}</li>
571-
))}
572-
</ul>
573-
);
574-
}
575-
```
576-
577-
```js src/Home.js hidden
578-
export default function Home() {
579-
return <p>Welcome to my profile!</p>;
580-
}
581-
```
582-
583-
```js src/data.js hidden
584-
let postsPromise;
585-
586-
export function getPosts() {
587-
if (!postsPromise) {
588-
postsPromise = loadPosts();
589-
}
590-
return postsPromise;
591-
}
592-
593-
async function loadPosts() {
594-
// Add a delay so that loading is easier to observe.
595-
await new Promise(resolve => setTimeout(resolve, 1500));
596-
597-
return Array.from({ length: 5 }, (_, index) => ({
598-
id: index,
599-
title: 'Post #' + (index + 1),
600-
}));
601-
}
602-
```
603-
604-
```css
605-
button {
606-
margin-right: 8px;
607-
}
608-
```
609-
610-
</Sandpack>
611-
612-
<Solution />
613-
614-
#### Pre-rendering the tab can avoid the fallback {/*pre-rendering-the-tab-can-avoid-the-fallback*/}
615-
616-
This version renders both tabs inside Activity boundaries. React begins rendering `<Posts>` while its boundary is hidden. Wait briefly before selecting **Posts**. The list appears immediately because the hidden render started loading it in the background.
516+
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.
617517

618518
<Sandpack>
619519

@@ -707,10 +607,6 @@ button {
707607

708608
</Sandpack>
709609

710-
<Solution />
711-
712-
</Recipes>
713-
714610
If the user selects Posts before the hidden render finishes, the Suspense fallback appears until the data is ready. Pre-rendering can reduce the loading time, but it does not guarantee that the content will always be ready.
715611

716612
<Note>

0 commit comments

Comments
 (0)