React Reference ยท Components and Special Elements
The Activity component
Reference for React Activity: hide parts of the UI while keeping their state, pause their effects, and pre-render screens at low priority with mode.
Activity lets you hide a part of the UI without throwing its state away. With mode="visible" the children render normally. With mode="hidden" React hides them (the DOM stays but is not displayed), keeps their state, cleans up their effects, and renders any updates to them at a low priority, when React has nothing more urgent to do.
It sits between two older approaches. Conditional rendering ({show && <Panel />}) unmounts the panel and loses its state. Hiding with CSS (display: none) keeps the state, but effects keep running and every update stays expensive. Activity keeps the state like CSS does, but stops the side effects like unmounting does.
Activity was added in React 19.2. In earlier versions it does not exist.
import { Activity } from 'react'; <Activity mode={isOpen ? 'visible' : 'hidden'}> <Sidebar /> </Activity>
Try it
In the first example, typing in Email, switching to Note and back leaves your text in place. The console shows Email: effect on, then Email: effect off when it is hidden, then Note: effect on, and so on. A tab that starts hidden (Note) has rendered but runs no effect until the first time it becomes visible.
In the second example, click both counters a few times, press Hide both, then Show both. The Activity counter has its number back; the conditionally rendered one starts from 0.
Syntax
<Activity mode={'visible' | 'hidden'}>{children}</Activity>
| Name | Description |
|---|---|
mode |
"visible" (default) renders children as normal. "hidden" hides them and pauses their effects while keeping their state. |
children |
The UI to show or hide. |
Returns: the children, wrapped in React's own bookkeeping. It adds no DOM element of its own.
Edge cases: mode should be one of the two strings; pass a literal or a conditional expression, not a boolean.
Usage notes
- Hidden means
display: none. React applies it to the top-level DOM nodes of the children, so they take no space and are not read by assistive technology. Content inside stays in the DOM. - Effects. When the mode becomes
hidden, effect cleanups (including layout effects) run. When it becomesvisibleagain, the effects run again. Subscriptions, timers and fetches stop while the content is hidden; a media element may need an explicit pause because React does not control playback. - Pre-rendering. Content that starts as
hiddenis rendered at low priority without running effects, so the UI is ready the instant the user navigates to it. Data that is read during that render can load in the background if it integrates with Suspense. - State survives, but memory is used. Hidden trees stay in memory. Do not wrap a huge list "just in case"; keep at most a handful of recently used screens.
- Updates are deprioritized. If a hidden component receives new props, React renders it when idle. Reading it on switch-back may still show a quick catch-up.
- Reset when needed. To discard the hidden state, change a
keyon the contents or unmount the Activity itself. - Not a security feature. Hidden content is still in the page; do not use
Activityto hide private data. - Mind effects with side outputs. An effect that logs analytics "viewed" on mount will fire every time the tab becomes visible, not just once.
Related
- Suspense, useEffect, ViewTransition
- Lessons: Animation, Conditional rendering