Sub topic 2
Meet Activity
This chapter introduces the Activity component from React 19.2. It covers what Activity does, how it differs from conditional rendering, and what a boundary looks like in code. The next chapters cover the mode prop, state preservation, and Effects in more detail.
Prerequisites
This chapter assumes you already know the following:
- React function components, props, and JSX.
useStateanduseEffect, including Effect cleanup functions.- The basics of mounting and unmounting. A component mounts when React first adds it to the tree. It unmounts when React removes it.
- You have React 19.2 or later. Earlier versions do not export
Activity.
The problem Activity solves
Most React apps hide UI with conditional rendering:
{showDraft && <Draft />}When showDraft becomes false, React unmounts Draft. Unmounting destroys the component's state and removes its DOM nodes. When showDraft becomes true again, React mounts a new Draft that starts from its initial state.
The example below shows this. Type something into the textarea, click the button twice, and the text is gone.
import { useState } from 'react';
function Draft() {
const [text, setText] = useState('');
return <textarea value={text} onChange={(e) => setText(e.target.value)} />;
}
export default function App() {
const [showDraft, setShowDraft] = useState(true);
return (
<>
<button onClick={() => setShowDraft(!showDraft)}>Toggle draft</button>
{showDraft && <Draft />}
</>
);
}This behavior is often what you want. A closed dialog should usually reset. But it is a poor fit for a tab the user will come back to, a half-written message, or a scroll position in a long list. People expect those to still be there.
Before Activity, you had a few workarounds. Each has a cost:
- Lift the state up. Move
textinto a parent that stays mounted. This works, but the parent now owns state it does not care about, and DOM state such as scroll position is still lost. - Hide with CSS. Render the component always and set
display: none. State and DOM survive, but the component keeps running its Effects and keeps rendering at full priority while the user cannot see it. - Store state externally. Save drafts in a store or in
localStorage. This adds code and does not restore DOM state.
Activity gives you a fourth option. The component stays mounted, so its state and DOM remain. React also treats it as hidden, which changes how its Effects and updates behave.
Meet Activity
Activity is a component you import from react. You wrap part of your UI in it and give it a mode prop.
import { Activity, useState } from 'react';
function Draft() {
const [text, setText] = useState('');
return <textarea value={text} onChange={(e) => setText(e.target.value)} />;
}
export default function App() {
const [showDraft, setShowDraft] = useState(true);
return (
<>
<button onClick={() => setShowDraft(!showDraft)}>Toggle draft</button>
<Activity mode={showDraft ? 'visible' : 'hidden'}>
<Draft />
</Activity>
</>
);
}Only the toggling line changed. Instead of {showDraft && <Draft />}, the code renders Draft inside an Activity and derives mode from the same boolean. Now type in the textarea, hide it, and show it again. The text is still there.
The Activity wrapper does not add an element to the page. It has no DOM node of its own. It is a boundary that React uses to decide how to treat everything inside it.
The two modes
The mode prop takes one of two strings:
| Mode | Children rendered | State kept | DOM | Effects |
|---|---|---|---|---|
| `'visible'` | Yes, normally | Yes | Shown | Running |
| `'hidden'` | Yes, at lower priority | Yes | Kept, but hidden from view | Cleaned up |
If you omit mode, it defaults to 'visible'.
The details of each column belong to later chapters. For now, remember the central idea: hidden does not mean unmounted. The component tree is still there, still holding its state. React has only paused the parts of it that interact with the outside world.
A mental model
Think of a component with two separate parts:
- Its memory. State, refs, and the DOM nodes it created.
- Its connections. Effects such as timers, subscriptions, and event listeners that reach outside the component.
Conditional rendering destroys both. CSS hiding keeps both alive. Activity in hidden mode keeps the memory and shuts down the connections. When the boundary becomes visible again, React reconnects them by running the Effects again.
A realistic example: tabs
Tabs are the most common use. Each panel has its own local state. Users expect to switch away and come back without losing what they typed.
import { Activity, useState } from 'react';
function Inbox() {
const [filter, setFilter] = useState('');
return <input value={filter} onChange={(e) => setFilter(e.target.value)} placeholder="Filter" />;
}
function Settings() {
const [name, setName] = useState('');
return <input value={name} onChange={(e) => setName(e.target.value)} placeholder="Name" />;
}
export default function Tabs() {
const [tab, setTab] = useState('inbox');
return (
<>
<button onClick={() => setTab('inbox')}>Inbox</button>
<button onClick={() => setTab('settings')}>Settings</button>
<Activity mode={tab === 'inbox' ? 'visible' : 'hidden'}>
<Inbox />
</Activity>
<Activity mode={tab === 'settings' ? 'visible' : 'hidden'}>
<Settings />
</Activity>
</>
);
}Each tab has its own Activity boundary, and each boundary has its own mode. Exactly one is visible at a time, but both are mounted. Type a filter in Inbox, switch to Settings, type a name, and switch back. Both inputs keep their values.
Compare this with the usual conditional version, {tab === 'inbox' && <Inbox />}. There, each switch would reset the input you just left.
Not every Activity is mounted at the start
One detail is easy to miss. A boundary that starts as hidden does not behave like one that was visible and then hidden. A hidden boundary can still render its children, but React does this at lower priority, after the visible content. This lets you prepare a screen before the user asks for it. The Pre-rendering chapter covers this.
For this chapter, the point is smaller: mode describes the current treatment of the subtree. It is not an event that fires once.
How Activity differs from the alternatives
| Approach | State survives | DOM survives | Effects while hidden | Cost while hidden |
|---|---|---|---|---|
| Conditional rendering | No | No | Not mounted | None |
| CSS `display: none` | Yes | Yes | Keep running | Full rendering and Effects |
| Lifting state up | Only the lifted state | No | Not mounted | Small |
| `Activity` hidden | Yes | Yes | Cleaned up | Memory, plus low-priority renders |
The Activity row is the reason the component exists. It is the only approach that keeps both state and DOM and also stops the Effects.
Common mistakes at this stage
- Using different boundaries for show and hide. Keep one
Activityper section and change only itsmode. If you moveDraftbetween a wrapped and unwrapped position, React treats it as a different component and resets its state. - Expecting everything to stop. Hidden means Effects are cleaned up. It does not stop code that runs during render, and it does not stop things that Effects never owned. For example, a
<video>element stays in the page, so a playing video can keep playing. Later chapters show how to handle this with cleanup. - Wrapping everything. Each hidden tree stays in memory. Use
Activitywhere users expect to return to the same state, not on every conditional. - Forgetting the version. On React older than 19.2,
import { Activity } from 'react'givesundefinedand the app fails when it renders the component.
What comes next
You now have the core idea: Activity marks a boundary, and mode decides whether that subtree is visible or hidden while staying mounted. The next chapter looks at mode itself, then at exactly what is preserved and what is cleaned up.
Exercise
You have a form in a side panel. It is currently rendered with {open && <Form />}, and users complain that the half-filled fields vanish when they close and reopen the panel. Rewrite the line with Activity, then name one thing that stays alive and one thing that React stops while the panel is closed.
Show answer
Replace it with <Activity mode={open ? 'visible' : 'hidden'}><Form /></Activity>. While hidden, the form's state (the typed field values) and its DOM stay in memory. React cleans up the Effects inside the form, such as subscriptions and timers, and runs them again when the panel becomes visible.
Exercise
A teammate says that hiding a component with display: none and hiding it with <Activity mode="hidden"> are the same thing because state survives in both. What difference would you point out?
Show answer
With display: none, the component stays fully active. Its Effects keep running and it keeps rendering at normal priority. With a hidden Activity, React cleans up the Effects and renders updates at lower priority, so a hidden timer or subscription stops doing work until the boundary is visible again.