Sub topic 2
Meet Activity
This chapter introduces the Activity component. It assumes you are comfortable with function components, props, and JSX. It also assumes you know useState, and useEffect with cleanup functions. You need React 19.2 or later, because Activity is not available in earlier versions.
The problem Activity solves
Conditional rendering with && removes a component from the tree when the condition is false. React unmounts it, which destroys its state and its DOM nodes. When the condition becomes true again, React mounts a fresh copy that starts from its initial state.
This example shows the loss. Type something in 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 />}
</>
);
}Hiding Draft unmounts it, so showing it again mounts a new Draft with an empty text.
Wrapping content in Activity
Activity is a component that marks a section of the UI whose state React should keep while the section is hidden. You import it from react and wrap the content you want to preserve. Its mode prop takes 'visible' or 'hidden'.
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>
</>
);
}The same boolean now drives mode instead of deciding whether Draft exists. Draft stays in the React tree the whole time. When mode is 'hidden', React hides the content, and the state of text is kept. Switch back to 'visible' and the textarea shows what you typed.
Conditional rendering compared with Activity
The two approaches differ in what happens at the moment the UI disappears.
| Aspect | `{show && <Draft />}` | `<Activity mode="hidden">` |
|---|---|---|
| Component state | Destroyed | Kept |
| DOM nodes | Removed | Kept in the page, but hidden |
| Effects | Cleaned up on unmount | Cleaned up while hidden, run again when visible |
| Showing it again | Fresh mount | Same instance, same state |
You will look at the state and DOM rows in detail in later chapters. The Effects row matters now, because it shows that Activity does not freeze everything. React treats a hidden boundary as if its Effects were unmounted, even though the component itself is still alive.
Using several boundaries
You can place more than one Activity on a page. Each boundary has its own mode, so each part of the UI keeps its own state independently. A tabbed interface is the common case. Each tab sits in its own boundary, and only the selected tab is visible.
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>
</>
);
}Type a filter in Inbox, switch to Settings, type a name, then switch back. Both inputs keep their values.
Common mistakes
- Forgetting the import.
Activityis a named export ofreact. Without the import, JSX treats it as an undefined component and React throws an error. - Using a React version before 19.2. The import is then
undefined, and rendering fails. Check the installed version first. - Expecting the hidden UI to stop doing everything. State is kept, but code that touches the DOM directly, such as media playback, can keep running. Later chapters cover this.
Exercise
A component is wrapped in <Activity mode={isOpen ? 'visible' : 'hidden'}>. A user types into an input inside it, then isOpen becomes false and later true again. What does the input contain, and why would {isOpen && <Panel />} give a different result?
Show answer
The input still contains the typed text. Activity keeps the component in the tree while it is hidden, so its useState value survives. With {isOpen && <Panel />}, React unmounts Panel when isOpen is false, which destroys its state. When isOpen becomes true, React mounts a new Panel and the input is empty.
Exercise
Change the Tabs example so that a third tab, Help, also keeps its state. What do you add?
Show answer
Add a Help component with its own useState. Add a third button that calls setTab('help'). Wrap <Help /> in its own <Activity mode={tab === 'help' ? 'visible' : 'hidden'}>.