How to Preserve Hidden UI State with React’s Activity
When you hide part of the UI with <Activity>, the state and the input field contents from before hiding are still there when you show it again. With conditional rendering such as {show && <Sidebar />}, the component is removed every time you hide it, and its state goes back to its initial value.
In my test environment, I hid the same component in two different ways and then showed it again. This article summarizes what happened to the state, the input field, and the Effects, together with the caveats from the official documentation.
The test environment was Ubuntu 24.04 with Node.js 22.22.0. I used react / react-dom 19.3.1, @types/react 19.3.0, typescript 6.0.3, esbuild 0.28.2, and jsdom 29.1.1. I type-checked the .tsx files with tsc, transformed them with esbuild, and rendered them in jsdom. For the <video> test and for recording the GIF, I used Chromium from Playwright 1.56.1.
The official <Activity> reference also says it can be used to split hydration of server-rendered HTML into smaller parts (speeding up interactions during page load). I did not test that use case in this article.
- 1. With conditional rendering, state is lost when you hide a component
- 2. Hiding and showing with the same steps: only the Activity version comes back intact
- 3. While hidden, Effects are cleaned up but rendering continues
- 4. What hiding does not stop: a playing video
- 5. Starting as hidden lets you render content before showing it
- 6. A component that returns only text renders nothing when hidden
- 7. Think of hidden content as "unmounted"
With conditional rendering, state is lost when you hide a component
<Activity> is a component introduced in React 19.2 (React 19.2 release post). The release post introduces it as an alternative to rendering parts of an app conditionally.
According to the example in the official reference that restores the state of hidden components, with conditional rendering the component is unmounted when it is hidden, and its internal state is lost.
For the test, I prepared the following component. It has a count state that increases with the +1 button, and an input field that is not backed by state. It also counts how many times its Effect is set up and cleaned up.
import { useEffect, useState } from 'react';
import { log } from './log';
export function Counter({ note }: { note: string }) {
const [count, setCount] = useState(0);
log.renders++;
useEffect(() => {
log.setups++; // corresponds to work like starting a subscription
return () => {
log.cleanups++; // corresponds to work like ending a subscription
};
}, []);
return (
<div>
<button id="inc" onClick={() => setCount((c) => c + 1)}>+1</button>
<span id="count">{count}</span>
<input id="draft" />
<span id="note">{note}</span>
</div>
);
}
The first approach is the usual one: render the component only when show is true.
import { Counter } from './Counter';
export function App({ show, note }: { show: boolean; note: string }) {
return <>{show && <Counter note={note} />}</>;
}
The second approach wraps the component in <Activity> and passes either 'visible' or 'hidden' to mode.
import { Activity } from 'react';
import { Counter } from './Counter';
export function App({ show, note }: { show: boolean; note: string }) {
return (
<Activity mode={show ? 'visible' : 'hidden'}>
<Counter note={note} />
</Activity>
);
}
Hiding and showing with the same steps: only the Activity version comes back intact
I ran both versions through the same steps. First, with the component visible, I clicked +1 three times and typed the Japanese word for "draft" into the input field. Next, I hid the component and, while it was hidden, changed the note prop from A to B. Finally, I showed it again.
renders is the number of times Counter rendered, and setups and cleanups are the number of times its Effect was set up and cleaned up. style is the style attribute of the outermost div of Counter.
$ node run.mjs ./out/AppConditional.mjs
./out/AppConditional.mjs
1. 表示して +1 を3回、入力欄に「下書き」: count=3 draft="下書き" note=A style="" | renders=4 setups=1 cleanups=0
2. 隠す: DOM なし | renders=4 setups=1 cleanups=1
3. 隠したまま note を B に: DOM なし | renders=4 setups=1 cleanups=1
4. 表示に戻す: count=0 draft="" note=B style="" | renders=5 setups=2 cleanups=1
$ node run.mjs ./out/AppActivity.mjs
./out/AppActivity.mjs
1. 表示して +1 を3回、入力欄に「下書き」: count=3 draft="下書き" note=A style="" | renders=4 setups=1 cleanups=0
2. 隠す: count=3 draft="下書き" note=A style="display: none !important;" | renders=5 setups=1 cleanups=1
3. 隠したまま note を B に: count=3 draft="下書き" note=B style="display: none !important;" | renders=6 setups=1 cleanups=1
4. 表示に戻す: count=3 draft="下書き" note=B style="" | renders=7 setups=2 cleanups=1
(The step labels in the output are in Japanese: 1. show it, click +1 three times, and type "draft" into the input; 2. hide it; 3. change note to B while hidden; 4. show it again. DOM なし means "no DOM".)
In the conditional rendering version, hiding the component removed its DOM entirely. When it was shown again, count was 0 and the input field was empty.
In the <Activity> version, when it was shown again, count was still 3 and the input field still contained the draft text. While hidden, the DOM stayed in place and was only made invisible with display: none !important;. The input field's contents are held by the DOM, not by React state, so they survive because the DOM survives.
The GIF below shows both approaches running one above the other in Chromium. The "+1" button at the top clicks the +1 button of the Counter in each of the two panels in turn. The Hide/Show toggle button (labeled in Japanese in the GIF) switches show for both panels. Before recording, I typed the same draft text into both input fields.
Right after each click, any field whose value changed gets a pink border and pink text. On the fifth click, the components are shown again: in the top panel (① conditional rendering, labeled in Japanese in the GIF), count goes back to 0 and the input field is empty. In the bottom panel (② hide with <Activity>, also labeled in Japanese), count 3 and the draft text are still there.

Simply replacing conditional rendering with <Activity mode={show ? 'visible' : 'hidden'}> preserves the hidden component's state and DOM. I did not change anything inside Counter.
While hidden, Effects are cleaned up but rendering continues
Looking at setups and cleanups in the output, even in the <Activity> version the Effect was cleaned up when the component was hidden (cleanups=1) and set up again when it was shown (setups=2). This part behaves the same as the conditional rendering version.
The official <Activity> reference also says that hiding destroys the children's Effects and cleans up their subscriptions, and showing them again recreates the Effects. Subscriptions do not keep running in a hidden screen.
The difference is rendering. When I changed note to B while the component was hidden, renders increased and the DOM's note also became B. According to the reference, hidden children still re-render in response to new props, but at a lower priority than the rest of the visible content. I did not verify this priority difference in my test.
What hiding does not stop: a playing video
If the DOM stays, then behavior driven by the DOM itself also continues. The reference's section on unwanted side effects in hidden components uses <video> as an example: unmounting it with conditional rendering stops the video, but hiding it with <Activity> keeps the element in the DOM, so playback continues.
I checked this in Chromium. The video was a 20-second test clip (no audio) generated with ffmpeg, set to muted so it could autoplay. plain is the version without cleanup, and pause is the version that calls pause() in its cleanup. Both were placed inside <Activity>, started playing, and were then hidden.
$ node video/run-video.mjs
plain: 再生して2秒 paused=false currentTime=2.0 display=inline
plain: 隠した直後 paused=false currentTime=2.0 display=none
plain: 隠して2秒後 paused=false currentTime=4.0 display=none
plain: 表示に戻した paused=false currentTime=4.0 display=inline
pause: 再生して2秒 paused=false currentTime=2.0 display=inline
pause: 隠した直後 paused=true currentTime=2.0 display=none
pause: 隠して2秒後 paused=true currentTime=2.0 display=none
pause: 表示に戻した paused=true currentTime=2.0 display=inline
(The labels mean: 2 seconds after starting playback; right after hiding; 2 seconds after hiding; shown again.)
The version without cleanup stayed at paused=false after being hidden, and the playback position advanced from 2 seconds to 4 seconds. It was just invisible; playback kept going.
A <video> hidden with <Activity> keeps playing unless you write cleanup for it. With a video that has audio, the sound keeps playing even though the video is not on screen (the video in my test had no audio).
The version that calls pause() in its cleanup switched to paused=true right after being hidden, and the playback position stayed at 2 seconds. When shown again, the position was still 2 seconds. However, playback did not resume automatically; it stayed at paused=true. Here is how the cleanup is written (an excerpt from the Video.tsx used in the test, with imports omitted):
function PausingVideo() {
const ref = useRef<HTMLVideoElement>(null);
useLayoutEffect(() => {
const video = ref.current!;
return () => {
video.pause(); // stop it when hidden
};
}, []);
return <video id="v" ref={ref} src="test.webm" muted playsInline />;
}
The reference explains that useLayoutEffect is used instead of useEffect because this cleanup is tied to the content becoming invisible on screen. With a regular Effect, the cleanup can be delayed, for example when a Suspense boundary suspends again or because of a View Transition. Besides <video>, the reference lists <audio> and <iframe> as elements that often need the same treatment. I did not test <audio> or <iframe> in my environment.
Starting as hidden lets you render content before showing it
<Activity> can also be used to render a screen in advance, before it is shown. According to the reference's section on pre-rendering content, children rendered as hidden from the start are rendered but do not appear on screen. Their Effects, however, do not run until they become visible.
My test matched this. When rendered as hidden from the start, Counter rendered once, and its DOM was created with display: none !important;. The Effect was set up only when the component became visible.
$ node prerender.mjs
最初から hidden: renders=1 setups=0 cleanups=0 DOM=<div style="display: none !important;"><button id="inc">+1</button><span id="count">0</span><input id="draft"><span id="note">A</span></div>
visible にした: renders=2 setups=1 cleanups=0 style=""
(最初から hidden means "hidden from the start", and visible にした means "switched to visible".)
According to the reference, data can be loaded during pre-rendering only when it is read through a Suspense-enabled method, such as a Promise read with use. Data fetched inside an Effect is not loaded until the content is shown. I did not test data loading.
A component that returns only text renders nothing when hidden
The caveats in the reference say that hiding a component that returns only text renders nothing, because there is no DOM element to apply display: none to.
$ node text.mjs
mode=hidden: innerHTML=""
mode=visible: innerHTML="Hello, World!"
mode=hidden: innerHTML=""
My test confirmed this: nothing was output while hidden, and the text appeared when the mode was set to visible.
Think of hidden content as "unmounted"
According to the reference's section on Effects in hidden components, children of a hidden <Activity> are conceptually unmounted. React just keeps their state for you; their Effects are not running.
So most components that already clean up their Effects properly can be hidden with <Activity> as they are. What you need to watch out for are elements whose DOM keeps doing something on its own, like the <video> above. The reference recommends using <StrictMode> to catch missing cleanup early.





