Skip to content

Commit 9f631f9

Browse files
committed
Revert "Use lightweight Activity preservation examples"
This reverts commit 4feee1b.
1 parent 4feee1b commit 9f631f9

1 file changed

Lines changed: 133 additions & 76 deletions

File tree

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

Lines changed: 133 additions & 76 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 in a hidden sidebar {/*restoring-the-state-of-hidden-components*/}
70+
### Preserving state while content is hidden {/*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,115 +172,95 @@ Changing `mode` preserves the state of the children. Removing the boundary or ch
172172

173173
---
174174

175-
### Preserving DOM state in an input {/*restoring-the-dom-of-hidden-components*/}
175+
### Preserving DOM state while content is hidden {/*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, hide the contact form, and then show it again. The `<textarea>` value remains because its DOM node was hidden rather than removed.
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.
180180

181181
<Sandpack>
182182

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

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

189189
return (
190190
<>
191-
<button
192-
onClick={() => setIsShowingContact(showing => !showing)}
191+
<div
192+
aria-label='Profile sections'
193+
className='section-buttons'
194+
role='group'
193195
>
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>
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'}
217+
>
218+
<Contact />
201219
</Activity>
202220
</>
203221
);
204222
}
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);
239223

224+
function SectionButton({ isActive, onClick, children }) {
240225
return (
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-
</>
226+
<button aria-pressed={isActive} onClick={onClick}>
227+
{children}
228+
</button>
249229
);
250230
}
251231

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

236+
function Contact() {
260237
return (
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-
/>
238+
<p>
239+
<label htmlFor='message'>Message</label>
240+
<textarea id='message' />
241+
</p>
268242
);
269243
}
270244
```
271245

272246
```css
273-
video {
274-
aspect-ratio: 16 / 9;
275-
margin-top: 12px;
276-
max-width: 100%;
277-
width: 400px;
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;
278258
}
279259
```
280260

281261
</Sandpack>
282262

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.
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.
284264

285265
---
286266

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

501481
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.
502482

503-
See [Preserving video playback while content is hidden](#preserving-video-playback-while-content-is-hidden) for a complete example.
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>
504561

505562
</Pitfall>
506563

0 commit comments

Comments
 (0)