Skip to content

Commit 4feee1b

Browse files
committed
Use lightweight Activity preservation examples
1 parent 2e9f13a commit 4feee1b

1 file changed

Lines changed: 76 additions & 133 deletions

File tree

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

Lines changed: 76 additions & 133 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ Activity is useful when part of the UI may become hidden and visible again. Unli
6767

6868
Use an Activity boundary when preserving that work is valuable—for example, for a tab the user is likely to revisit or a panel that can prepare data in the background. A hidden boundary retains its state and DOM nodes, so it continues using memory. If the content is unlikely to become visible again, conditionally rendering it may be preferable because unmounting allows React and the browser to release its resources.
6969

70-
### Preserving state while content is hidden {/*restoring-the-state-of-hidden-components*/}
70+
### Preserving state in a hidden sidebar {/*restoring-the-state-of-hidden-components*/}
7171

7272
When this condition becomes false, React removes `<Sidebar>` from the tree and discards its state:
7373

@@ -172,95 +172,115 @@ Changing `mode` preserves the state of the children. Removing the boundary or ch
172172

173173
---
174174

175-
### Preserving DOM state while content is hidden {/*restoring-the-dom-of-hidden-components*/}
175+
### Preserving DOM state in an input {/*restoring-the-dom-of-hidden-components*/}
176176

177177
An Activity boundary also preserves state held by the browser in DOM nodes. This includes an uncontrolled input's current value, scroll position, and media playback position.
178178

179-
In this example, enter a draft in the Contact section, switch sections, and then return to Contact. The `<textarea>` value remains because its DOM node was hidden rather than removed.
179+
In this example, enter a draft, hide the contact form, and then show it again. The `<textarea>` value remains because its DOM node was hidden rather than removed.
180180

181181
<Sandpack>
182182

183183
```js
184184
import { Activity, useState } from 'react';
185185

186186
export default function App() {
187-
const [activeSection, setActiveSection] = useState('contact');
187+
const [isShowingContact, setIsShowingContact] = useState(true);
188188

189189
return (
190190
<>
191-
<div
192-
aria-label='Profile sections'
193-
className='section-buttons'
194-
role='group'
195-
>
196-
<SectionButton
197-
isActive={activeSection === 'home'}
198-
onClick={() => setActiveSection('home')}
199-
>
200-
Home
201-
</SectionButton>
202-
<SectionButton
203-
isActive={activeSection === 'contact'}
204-
onClick={() => setActiveSection('contact')}
205-
>
206-
Contact
207-
</SectionButton>
208-
</div>
209-
210-
<Activity
211-
mode={activeSection === 'home' ? 'visible' : 'hidden'}
212-
>
213-
<Home />
214-
</Activity>
215-
<Activity
216-
mode={activeSection === 'contact' ? 'visible' : 'hidden'}
191+
<button
192+
onClick={() => setIsShowingContact(showing => !showing)}
217193
>
218-
<Contact />
194+
{isShowingContact ? 'Hide' : 'Show'} contact form
195+
</button>
196+
<Activity mode={isShowingContact ? 'visible' : 'hidden'}>
197+
<p>
198+
<label htmlFor='message'>Message</label>
199+
<textarea id='message' />
200+
</p>
219201
</Activity>
220202
</>
221203
);
222204
}
205+
```
206+
207+
```css
208+
label,
209+
textarea {
210+
display: block;
211+
}
212+
textarea {
213+
margin-top: 4px;
214+
}
215+
```
216+
217+
</Sandpack>
218+
219+
Preserving DOM state also means that DOM behavior can continue while content is hidden. See [Troubleshooting](#my-hidden-components-have-unwanted-side-effects) for DOM behavior that requires explicit cleanup.
220+
221+
---
222+
223+
### Preserving video playback while content is hidden {/*preserving-video-playback-while-content-is-hidden*/}
224+
225+
Browser-managed state in DOM nodes also includes a video's playback position. Play the video, hide it, and then show it again. The video pauses while hidden and returns at the same playback position.
226+
227+
<Sandpack>
228+
229+
```js
230+
import {
231+
Activity,
232+
useLayoutEffect,
233+
useRef,
234+
useState,
235+
} from 'react';
236+
237+
export default function App() {
238+
const [isShowingVideo, setIsShowingVideo] = useState(true);
223239

224-
function SectionButton({ isActive, onClick, children }) {
225240
return (
226-
<button aria-pressed={isActive} onClick={onClick}>
227-
{children}
228-
</button>
241+
<>
242+
<button onClick={() => setIsShowingVideo(showing => !showing)}>
243+
{isShowingVideo ? 'Hide' : 'Show'} video
244+
</button>
245+
<Activity mode={isShowingVideo ? 'visible' : 'hidden'}>
246+
<VideoPlayer />
247+
</Activity>
248+
</>
229249
);
230250
}
231251

232-
function Home() {
233-
return <p>Welcome to my profile!</p>;
234-
}
252+
function VideoPlayer() {
253+
const ref = useRef(null);
254+
255+
useLayoutEffect(() => {
256+
const video = ref.current;
257+
return () => video.pause();
258+
}, []);
235259

236-
function Contact() {
237260
return (
238-
<p>
239-
<label htmlFor='message'>Message</label>
240-
<textarea id='message' />
241-
</p>
261+
<video
262+
aria-label='Big Buck Bunny video'
263+
controls
264+
playsInline
265+
ref={ref}
266+
src='https://archive.org/download/BigBuckBunny_124/Content/big_buck_bunny_720p_surround.mp4'
267+
/>
242268
);
243269
}
244270
```
245271

246272
```css
247-
.section-buttons {
248-
display: flex;
249-
gap: 8px;
250-
margin-bottom: 12px;
251-
}
252-
label,
253-
textarea {
254-
display: block;
255-
}
256-
textarea {
257-
margin-top: 4px;
273+
video {
274+
aspect-ratio: 16 / 9;
275+
margin-top: 12px;
276+
max-width: 100%;
277+
width: 400px;
258278
}
259279
```
260280

261281
</Sandpack>
262282

263-
Preserving DOM state also means that DOM behavior can continue while content is hidden. See [Troubleshooting](#my-hidden-components-have-unwanted-side-effects) for DOM behavior that requires explicit cleanup.
283+
The cleanup pauses playback when the boundary becomes hidden. Because Activity preserves the `<video>` DOM node, it does not reset the playback position. See [Troubleshooting](#my-hidden-components-have-unwanted-side-effects) for why the cleanup is necessary.
264284

265285
---
266286

@@ -480,84 +500,7 @@ React runs the cleanup when an enclosing Activity boundary becomes hidden. Becau
480500

481501
The `useLayoutEffect` cleanup runs as part of hiding the UI. A cleanup from `useEffect` can run later if, for example, a Suspense boundary suspends or a View Transition is in progress.
482502

483-
This complete example pauses the video when you switch to Home while preserving its playback position. When you return to Video, playback can continue from the same position without recreating the element or downloading it again.
484-
485-
<Sandpack>
486-
487-
```js src/App.js active
488-
import {
489-
Activity,
490-
useLayoutEffect,
491-
useRef,
492-
useState,
493-
} from 'react';
494-
495-
export default function App() {
496-
const [activeTab, setActiveTab] = useState('video');
497-
498-
return (
499-
<>
500-
<div aria-label='Media sections' role='group'>
501-
<button
502-
aria-pressed={activeTab === 'home'}
503-
onClick={() => setActiveTab('home')}
504-
>
505-
Home
506-
</button>
507-
<button
508-
aria-pressed={activeTab === 'video'}
509-
onClick={() => setActiveTab('video')}
510-
>
511-
Video
512-
</button>
513-
</div>
514-
515-
<Activity mode={activeTab === 'home' ? 'visible' : 'hidden'}>
516-
<p>Welcome home!</p>
517-
</Activity>
518-
<Activity mode={activeTab === 'video' ? 'visible' : 'hidden'}>
519-
<VideoPlayer />
520-
</Activity>
521-
</>
522-
);
523-
}
524-
525-
function VideoPlayer() {
526-
const ref = useRef(null);
527-
528-
useLayoutEffect(() => {
529-
const video = ref.current;
530-
531-
return () => {
532-
video.pause();
533-
};
534-
}, []);
535-
536-
return (
537-
<video
538-
aria-label='Big Buck Bunny video'
539-
controls
540-
playsInline
541-
ref={ref}
542-
src='https://archive.org/download/BigBuckBunny_124/Content/big_buck_bunny_720p_surround.mp4'
543-
/>
544-
);
545-
}
546-
```
547-
548-
```css
549-
button {
550-
margin-right: 8px;
551-
}
552-
video {
553-
aspect-ratio: 16 / 9;
554-
margin-top: 12px;
555-
max-width: 100%;
556-
width: 400px;
557-
}
558-
```
559-
560-
</Sandpack>
503+
See [Preserving video playback while content is hidden](#preserving-video-playback-while-content-is-hidden) for a complete example.
561504

562505
</Pitfall>
563506

0 commit comments

Comments
 (0)