The short answer

#

Wrap the part of the UI you want to hide in <Activity> and switch its mode prop between "visible" and "hidden". React keeps the component's state and DOM, hides it with display: none, and pauses its Effects until it is shown again. Activity is stable from React 19.2.


_17
import { Activity, useState } from 'react';
_17
_17
function Tabs() {
_17
const [tab, setTab] = useState<'posts' | 'settings'>('posts');
_17
_17
return (
_17
<>
_17
<TabButtons current={tab} onChange={setTab} />
_17
<Activity mode={tab === 'posts' ? 'visible' : 'hidden'}>
_17
<Posts />
_17
</Activity>
_17
<Activity mode={tab === 'settings' ? 'visible' : 'hidden'}>
_17
<Settings />
_17
</Activity>
_17
</>
_17
);
_17
}

Why it happens

#

The usual way to hide part of a page is conditional rendering, such as {tab === 'posts' && <Posts />}. When the condition becomes false, React unmounts the component and throws its state away. Switch back and anything the user typed, any expanded rows and any data held in state are gone.

The old alternative was to keep everything mounted and hide it with CSS. State survives, but the hidden components keep running their Effects (timers, subscriptions, polling) and re-render at full priority whenever their props change, which slows down the screen the user is actually looking at.

Activity sits between the two. When mode is "hidden", React:

  • hides the children with display: none,
  • keeps their state and their DOM,
  • cleans up their Effects, as if they had unmounted,
  • renders any updates to them at a low priority, after visible work.

When mode goes back to "visible", React re-creates the Effects and shows the saved UI straight away.

How to fix it

#

Replace conditional rendering for screens people return to

Tabs, sidebars, drawers and multi-step forms are the best fit. Swap the && condition for an Activity with a mode, as in the short answer. The mode prop defaults to "visible" if you leave it out.

Pre-render a screen the user is likely to open next

An Activity that starts hidden still renders its children, at low priority, but does not run their Effects. You can use this to prepare the next step of a flow in the background:


_10
<Activity mode={step === 2 ? 'visible' : 'hidden'}>
_10
<ShippingForm />
_10
</Activity>

If the hidden component reads data through Suspense (for example with use and a cached promise, or a framework loader), that data can start loading before the user clicks. Data fetched inside useEffect will not load while hidden, because Effects do not run.

Give your Effects proper cleanup

Hiding runs your cleanup functions, so any Effect without one will leave things running. Some DOM elements also keep working when hidden with display: none. A playing video is the classic case, so pause it in a cleanup:


_12
function VideoPlayer({ src }: { src: string }) {
_12
const ref = useRef<HTMLVideoElement>(null);
_12
_12
useLayoutEffect(() => {
_12
const video = ref.current;
_12
return () => {
_12
video?.pause();
_12
};
_12
}, []);
_12
_12
return <video ref={ref} src={src} controls />;
_12
}

Common pitfalls

#
  • Expecting hidden Effects to keep running. A chat subscription inside a hidden Activity stops. If it must keep running, move it to a component above the Activity.
  • Hiding lots of large trees. Hidden content still uses memory and DOM nodes. Use Activity for a handful of screens, not for every row in a long list.
  • Forgetting it is not a security boundary. Hidden content is still in the DOM. Do not render data the current user should not see and rely on Activity to hide it.
  • Using an older React. Activity shipped as a stable API in React 19.2. Before that it only existed in experimental builds, so check your react version if the import fails.
#

quick question

Was this resource helpful?