Skip to main content

<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 />}
Conditional rendering removes the subtree
1Sidebar visible
2Condition becomes false
3Component unmounts
4Local state is discarded
5Later mount starts fresh

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>
Activity hide and restore lifecycle
1Visible
2Switch to hidden
3DOM is hidden
4Component state is retained
5Effects are cleaned up
6Visible again
7State restored + Effects re-created

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.

Think: state retained, activity paused
State
Retained for restoration.
DOM
Hidden rather than destroyed in the normal element case.
Effects
Cleaned up while hidden and re-created when visible.

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>
CSS hiding vs Activity
CSS display: none
Browser visual concern. React does not receive a hidden/restorable lifecycle signal.
Activity hidden
React knows the subtree is hidden, can preserve state, defer hidden work, and clean up Effects.

Activity vs conditional rendering​

RequirementConditional renderActivity
Remove UI entirelystrong fitnot primary goal
Reset local state when reopenedstrong fitwrong fit
Preserve local staterequires another owner/persistencestrong fit
React knows subtree is hiddennoyes
Prepare likely future UIlimiteduseful

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>
Preparing likely next UI
1Reports Activity hidden
2React renders hidden work at lower priority
3Code/data resources can start preparing
4User opens Reports
5Prepared subtree can reveal faster

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.

What Activity keeps alive
Benefit
State restoration Β· preserved DOM state Β· faster likely-next interactions.
Cost
Memory retention Β· hidden stale state Β· broader retained trees Β· possible background preparation work.

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​

Should this hidden UI use Activity?
Should local state reset when hidden?Conditional render / unmount
Should state and DOM state be restorable?Activity is a candidate
Restoration not worth retained memory?Lift only the state that truly must survive
Likely next screen can benefit from preparation?Activity + Suspense resources may help
Framework already owns route preservation?Prefer the framework model first

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​

Next​

Continue with Metadata and Resource Loading.