Skip to content
Advanced PatternsChapter 31 / 37Advanced5 min read

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 renderingWith Activity
isOpen && Component, unmounts when hiddenmode hidden, the component stays alive
State and input text are lostState and DOM are preserved
Loads again when it comes backContinues instantly from where it was
The next screen is not ready in advanceLow-priority pre-render while hidden

3. Where is it used?

Use caseBenefit
TabsSwitch tabs and the other tab's form text is still there
Sidebar or drawerClose and reopen it and the expanded sections are the same
Pre-render the next pageData and code are ready before the user clicks
Back navigationThe previous screen returns with its state
Video or chat panelEffects 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.

From visible to hidden and back
mode visible: the UI is shown, Effects are active
mode hidden: display none
Effects are cleaned up, state and DOM are preserved
Updates keep rendering at low priority
mode visible: Effects are set up again, the UI returns instantly

5. Code example

Example 1: Tabs that remember their state

jsx
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?

PartMeaning
import { Activity }A built-in component from React 19.2
mode={...}Only "visible" or "hidden"
Notes stateThe text is still there after you visit the Profile tab
Hidden tabIn the DOM but display: none

Example 2: How Effects behave

jsx
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

Final quick revision

PointDetail
Importimport { Activity } from "react"
Modes"visible" (default) and "hidden"
Hiddendisplay: none, Effects cleaned up, state preserved
BackgroundHidden children render at low priority
Use casesTabs, drawers, pre-render, back navigation