<Activity> in React 19.2
<Activity> lets React hide and later restore a subtree while preserving its internal state.
import {Activity} from 'react';
<Activity mode={isOpen ? 'visible' : 'hidden'}>
<Sidebar />
</Activity>
Its purpose is not simply βhide some DOM.β It gives React an explicit hidden/restorable lifecycle for the subtree.
Conditional rendering usually unmountsβ
{isOpen && <Sidebar />}
That is often exactly what you want when leaving the UI should also reset its state.
Activity preserves stateβ
<Activity mode={isOpen ? 'visible' : 'hidden'}>
<Sidebar />
</Activity>
This is useful for tabs, sidebars, panels, and workflows where temporary hiding should not mean βforget everything.β
Hidden modeβ
React 19.2 supports:
<Activity mode="visible">...</Activity>
<Activity mode="hidden">...</Activity>
In hidden mode, React visually hides the subtree using display: none behaviour. Its state remains available for restoration, while hidden work is treated differently from visible UI.
State preservation exampleβ
function FiltersPanel() {
const [advanced, setAdvanced] = useState(false);
return (
<label>
<input
type="checkbox"
checked={advanced}
onChange={event => setAdvanced(event.target.checked)}
/>
Advanced filters
</label>
);
}
Conditional rendering:
{showFilters && <FiltersPanel />}
can reset the local checkbox state after unmount/remount.
Activity:
<Activity mode={showFilters ? 'visible' : 'hidden'}>
<FiltersPanel />
</Activity>
can restore the previous local state when the panel becomes visible again.
Hidden Effects are cleaned upβ
State preservation does not mean all side effects remain active.
That prevents hidden UI from keeping unnecessary subscriptions, timers, observers, event listeners, or external connections active.
Correct cleanup matters:
useEffect(() => {
const connection = connect(roomId);
connection.start();
return () => {
connection.stop();
};
}, [roomId]);
Activity vs CSS hidingβ
CSS-only hiding preserves a mounted subtree, but React still sees that subtree as ordinarily mounted.
<div style={{display: isOpen ? 'block' : 'none'}}>
<Sidebar />
</div>
Activity vs conditional renderingβ
| Requirement | Conditional render | Activity |
|---|---|---|
| Remove UI entirely | strong fit | not primary goal |
| Reset local state when reopened | strong fit | wrong fit |
| Preserve local state | requires another owner/persistence | strong fit |
| React knows subtree is hidden | no | yes |
| Prepare likely future UI | limited | useful |
Pre-rendering likely future contentβ
A hidden Activity can prepare content before it becomes visible.
<Suspense fallback={<PageSkeleton />}>
<Activity mode={activeTab === 'reports' ? 'visible' : 'hidden'}>
<Reports />
</Activity>
</Suspense>
Pre-rendering is most useful when the future interaction is likely enough to justify the retained work.
Activity + Suspenseβ
Activity becomes especially useful with Suspense-enabled resources:
<Suspense fallback={<LoadingReports />}>
<Activity mode={showReports ? 'visible' : 'hidden'}>
<Reports />
</Activity>
</Suspense>
A hidden subtree can begin rendering and encounter supported suspending resources before the user makes it visible.
Activity does not magically make Effect-based fetching Suspense-aware:
useEffect(() => {
fetch('/api/reports').then(...);
}, []);
Use Suspense-aware resources when pre-rendering data during render is the goal.
Example: tab state preservationβ
function Dashboard() {
const [tab, setTab] = useState('overview');
return (
<>
<nav>
<button onClick={() => setTab('overview')}>Overview</button>
<button onClick={() => setTab('reports')}>Reports</button>
</nav>
<Activity mode={tab === 'overview' ? 'visible' : 'hidden'}>
<Overview />
</Activity>
<Activity mode={tab === 'reports' ? 'visible' : 'hidden'}>
<Reports />
</Activity>
</>
);
}
Local filters or draft DOM state in each tab can survive temporary hiding.
Memory and stale-state trade-offsβ
Preservation is not free.
Do not wrap every route or panel in Activity βjust in case.β
If leaving checkout should discard a draft, preserving that subtree may be the wrong product behaviour. Unmounting or an explicit reset is clearer.
DOM side effects need careβ
A hidden Activity preserves DOM, so DOM elements with their own side effectsβsuch as playing mediaβmay need explicit cleanup tied to the hidden lifecycle.
Design the component so its Effects or layout Effects clean up what should stop when the UI becomes hidden.
Accessibilityβ
Activity does not replace focus management or component semantics. You still need to design tab semantics, modal focus behaviour, announcements, keyboard navigation, and other accessibility requirements around the actual user experience.
Activity and routersβ
Framework routers may already own route caching, navigation, data, streaming, scroll restoration, and lifecycle. Do not layer Activity-based route preservation on top without understanding the framework's model and memory costs.
Production decision guideβ
Common mistakesβ
- Using Activity where product behaviour should reset state.
- Assuming hidden Effects keep running normally.
- Treating Activity as generic CSS
display: none. - Pre-rendering too many future screens.
- Expecting Effect-based fetching to become Suspense-aware.
- Ignoring memory, stale-state, and accessibility trade-offs.
Interview questionsβ
Junior: How does Activity differ from conditionally rendering a component?
Mid-level: What happens to state and Effects when an Activity becomes hidden?
Senior: What are the memory, DOM, and data-lifecycle trade-offs of using Activity for tab or route preservation?
Referencesβ
- https://react.dev/reference/react/Activity
- https://react.dev/blog/2025/10/01/react-19-2
- https://react.dev/reference/react/Suspense
Nextβ
Continue with Metadata and Resource Loading.