Sub topic 3
Switching between visible and hidden
This chapter assumes you know React function components, props, and JSX. It also assumes you know useState, and useEffect with cleanup functions. You need React 19.2 or later, which includes Activity.
The mode prop
Activity takes one prop that matters here: mode. It accepts two values.
'visible': the children render normally, and the user sees them.'hidden': the children are hidden from the screen, but React keeps them alive.
If you leave mode out, React treats it as 'visible'. In practice you almost always set it explicitly from your own state.
The usual pattern is to compute mode from the same condition you would have used for conditional rendering:
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>
</>
);
}Compare this with {showDraft && <Draft />}. In the conditional version, Draft leaves the tree when showDraft is false, so its text state is discarded. Here Draft stays in the tree. Only mode changes, so the textarea still holds what the user typed when it comes back.
What a mode change does
Changing mode does not mount or unmount the children. It moves them between two states.
Because the diagram syntax requires its own fence, here is the transition as a diagram:
Each transition has a clear effect on what React does:
| Part of the subtree | Going to hidden | Going back to visible |
|---|---|---|
| Component state | Kept | Still there |
| DOM nodes | Kept, but not shown | Shown again |
| Effects | Cleanup runs | Effects run again |
The next chapters cover state, the DOM, and Effects in detail. For now, remember the rule that a mode change is a switch, not a mount or unmount.
Several boundaries, one active at a time
You can place more than one Activity in the same parent. Each boundary has its own mode, so each one can be controlled separately. Tabs are a natural fit: only one panel is visible, and every other panel is hidden.
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 something in the Inbox filter, switch to Settings, type a name, then switch back. Both inputs keep their text. With conditional rendering, each switch would clear the input you left.
Common errors
- Passing a boolean.
mode={showDraft}is wrong.modeexpects the strings'visible'or'hidden', so convert the boolean with a ternary. - Expecting a hidden component to stop existing. A hidden child is still mounted. Code that assumes "not on screen means not mounted" will behave differently inside
Activity. - Forgetting cleanup. Effects are cleaned up when the boundary becomes hidden. If your Effect has no cleanup, a timer or subscription can keep running while the user cannot see the screen.
- Sharing one boundary across tabs. A single
Activitywith a changing child behaves like conditional rendering, because the child's identity changes. Give each screen its own boundary.
Exercise
Exercise
A component Panel holds a counter in useState. You render it as {open && <Panel />} and users complain that the counter resets each time they reopen the panel. Rewrite the line using Activity so the counter survives, and say what mode value is used when open is false.
Show answer
Use <Activity mode={open ? 'visible' : 'hidden'}><Panel /></Activity>. When open is false the mode is 'hidden'. Panel stays mounted, so its counter state is kept and is still there when the mode returns to 'visible'.
Exercise
Why does <Activity mode={showDraft}> fail to do what you want, and how do you fix it?
Show answer
mode takes the strings 'visible' or 'hidden', not a boolean. Fix it with mode={showDraft ? 'visible' : 'hidden'}.