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 b119ed4
Browse filesBrowse the repository at this point in the historyBrowse files
In this example, expand the sidebar, hide it, and show it again. The expanded state is preserved.
88
+
When the boundary becomes visible again, `<Sidebar>` resumes with its previous state.
89
+
90
+
The following example has a sidebar with an expandable Overview section and a button that hides and shows the sidebar. To verify that its state is preserved, expand the Overview section, hide the sidebar, and then show it again. The Overview section remains expanded.
87
91
88
92
<Sandpack>
89
93
@@ -177,7 +181,7 @@ Changing `mode` preserves the state of the children. Removing the boundary or ch
177
181
178
182
### Preserving DOM state while content is hidden {/*restoring-the-dom-of-hidden-components*/}
179
183
180
-
An Activity boundary also preserves state held by the browser in DOM nodes.
184
+
Because Activity boundaries hide their children without removing them, the children's DOM is also preserved. This is useful for maintaining state held by the browser in DOM nodes.
181
185
182
186
The Contact tab in the following examples contains an uncontrolled `<textarea>`. Enter a draft, switch to the Home tab, and then return to Contact to compare the behavior with and without Activity.
183
187
@@ -349,7 +353,80 @@ b {
349
353
350
354
</Recipes>
351
355
352
-
Activity also preserves other browser-managed state, such as scroll position and media playback position. In this example, play the video, hide it, and then show it again. When you play it again, the video continues from the same position.
356
+
Activity also preserves other browser-managed state, such as scroll position and media playback position. The following examples use the same video player to compare removing and hiding a `<video>` element. Play the video, hide it, show it again, and then select **Play**.
357
+
358
+
<RecipestitleText="The difference between removing and hiding a video"titleId="examples-preserving-media-state">
359
+
360
+
#### Removing the video resets its playback position {/*removing-the-video-resets-its-playback-position*/}
361
+
362
+
This version conditionally renders the video player. Hiding it removes the `<video>` element from the DOM, so playback starts from the beginning when you show it again.
#### Hiding the video preserves its playback position {/*hiding-the-video-preserves-its-playback-position*/}
428
+
429
+
This version renders the video player inside an Activity boundary. Hiding it preserves the `<video>` element, so playback continues from the same position when you show it and select **Play** again.
353
430
354
431
<Sandpack>
355
432
@@ -414,7 +491,11 @@ export default function VideoPlayer() {
414
491
415
492
</Sandpack>
416
493
417
-
The ref provides access to the `<video>` DOM node; it does not store the playback position. The `useLayoutEffect` cleanup pauses the video when the boundary becomes hidden. Activity preserves the DOM node and its playback position, but React detaches the ref while the boundary is hidden and reconnects it when the boundary becomes visible. See [Troubleshooting](#my-hidden-component-keeps-playing-audio-or-video) for more about cleaning up browser-managed behavior.
494
+
<Solution />
495
+
496
+
</Recipes>
497
+
498
+
The ref provides access to the `<video>` DOM node. It does not store the playback position. The `useLayoutEffect` cleanup pauses the video when the player is removed or its Activity boundary becomes hidden. Activity preserves the DOM node and its playback position, but React detaches the ref while the boundary is hidden and reconnects it when the boundary becomes visible. See [Troubleshooting](#my-hidden-component-keeps-playing-audio-or-video) for more about cleaning up browser-managed behavior.
418
499
419
500
---
420
501
@@ -430,7 +511,207 @@ An Activity boundary can also prepare content before the user sees it. Content i
430
511
</Suspense>
431
512
```
432
513
433
-
If `Posts` suspends while reading code or data, React continues rendering the rest of the page. If the hidden work finishes before the user selects Posts, React can reveal it without showing the Suspense fallback. Otherwise, the nearest fallback appears until the code or data is ready.
514
+
If `Posts` suspends while reading code or data, React continues rendering the rest of the page while the hidden work proceeds.
515
+
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
+
<RecipestitleText="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.
#### 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.
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.
0 commit comments