Activity Component
The React Activity component from React 19.2. Hide UI with visible and hidden modes, keep its state, and pre-render what the user will see next.
On this page
1. What is the Activity component?
You import Activity from react and give it a mode prop: "visible" or "hidden". The default is "visible".
In hidden mode the children disappear from the screen (CSS display: none) and their Effects are cleaned up, but their state and DOM are kept. When you make it visible again, everything continues from where it was.
2. Why does it matter?
The old way was to remove a component with conditional rendering. That unmounted the component and its state, such as form text or scroll position, was lost.
| Conditional rendering | With Activity |
|---|---|
| isOpen && Component, unmounts when hidden | mode hidden, the component stays alive |
| State and input text are lost | State and DOM are preserved |
| Loads again when it comes back | Continues instantly from where it was |
| The next screen is not ready in advance | Low-priority pre-render while hidden |
3. Where is it used?
| Use case | Benefit |
|---|---|
| Tabs | Switch tabs and the other tab's form text is still there |
| Sidebar or drawer | Close and reopen it and the expanded sections are the same |
| Pre-render the next page | Data and code are ready before the user clicks |
| Back navigation | The previous screen returns with its state |
| Video or chat panel | Effects stop while hidden, state is kept |
4. How does it work?
React still renders the children of a hidden Activity, but at low priority, so the visible UI does not slow down. Effects run only while it is visible.
5. Code example
Example 1: Tabs that remember their state
import { Activity, useState } from "react";
function Notes() {
const [text, setText] = useState("");
return (
<textarea value={text} onChange={(e) => setText(e.target.value)} />
);
}
function Profile() {
return <p>Profile details</p>;
}
export default function Tabs() {
const [tab, setTab] = useState("notes");
return (
<div>
<button onClick={() => setTab("notes")}>Notes</button>
<button onClick={() => setTab("profile")}>Profile</button>
<Activity mode={tab === "notes" ? "visible" : "hidden"}>
<Notes />
</Activity>
<Activity mode={tab === "profile" ? "visible" : "hidden"}>
<Profile />
</Activity>
</div>
);
}How does the code work?
| Part | Meaning |
|---|---|
import { Activity } | A built-in component from React 19.2 |
mode={...} | Only "visible" or "hidden" |
Notes state | The text is still there after you visit the Profile tab |
| Hidden tab | In the DOM but display: none |
Example 2: How Effects behave
import { useEffect } from "react";
function LiveClock() {
useEffect(() => {
const id = setInterval(() => console.log("tick"), 1000);
return () => clearInterval(id);
}, []);
return <p>The clock is running</p>;
}When LiveClock goes inside a hidden Activity, the cleanup runs and the interval stops. When it becomes visible, the Effect is set up again. That is why writing Effect cleanup correctly matters.
Interview tip
Remember the difference: when you hide with CSS, Effects keep running, and with conditional rendering the state is lost. Activity is the middle path: state is kept and Effects are paused.
6. Common mistakes
Mistake: Not writing Effect cleanup
Why? React calls the cleanup when the UI is hidden, and without cleanup, subscriptions and timers leak.
Better: Every Effect that subscribes to or starts something should return a cleanup function.
Mistake: Letting video or audio keep playing in hidden UI
Why? The DOM is preserved, so the media element does not pause on its own.
Better: Pause the media in the cleanup of useLayoutEffect so it stops as soon as the UI is hidden.
Mistake: Putting everything inside Activity
Why? Hidden UI still uses memory and renders in the background.
Better: Use it only where keeping state or pre-rendering truly helps, and use normal conditional rendering elsewhere.
7. What to learn before and after
| Stage | Topics |
|---|---|
| Before | Conditional Rendering, State with useState, useEffect |
| Alongside | Suspense & lazy(), useTransition & useDeferredValue |
| After | Server Components, Routing with React Router |
Final quick revision
| Point | Detail |
|---|---|
| Import | import { Activity } from "react" |
| Modes | "visible" (default) and "hidden" |
| Hidden | display: none, Effects cleaned up, state preserved |
| Background | Hidden children render at low priority |
| Use cases | Tabs, drawers, pre-render, back navigation |