Sub topic 4
What stays: state and DOM while hidden
This chapter assumes you know useState, useEffect and Effect cleanup, and that you run React 19.2 or later, which includes Activity. It covers what React keeps when an Activity boundary is hidden. The next chapter covers what React removes.
Two things survive hiding
When mode is 'hidden', React keeps two things for everything inside the boundary:
- State. Every
useState,useReduceranduseRefvalue stays in memory. The component is not unmounted, so React does not reset anything. - DOM. React keeps the DOM nodes in the page and hides them. In the browser they get
display: none, so the user can't see them. Their internal state stays too, including what the user typed into an input and the scroll position inside a panel.
Compare this with conditional rendering. In {show && <Draft />}, a false value unmounts Draft. React throws away its state and removes its DOM nodes. When show becomes true again, Draft mounts fresh with an empty textarea.
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>
</>
);
}Type something, click the button twice, and the text is still there. Draft was never unmounted.
Preserving state in a tabbed interface
Tabs are the usual case for this. Give each panel its own Activity boundary and drive each mode from the active tab.
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 text. Each component's state lives in its own boundary and does not depend on which tab is showing.
What "preserved" means in practice
The preserved DOM is the same set of nodes, not a copy. That has a few consequences:
| Item | While hidden |
|---|---|
| `useState` and `useReducer` values | Kept |
| `useRef` values and ref targets | Kept |
| DOM nodes and form field contents | Kept in the page, not displayed |
| Scroll position inside hidden panels | Kept by the DOM nodes |
| Effects | Cleaned up (next chapter) |
| Parent state outside the boundary | Not affected, since it never belonged to the boundary |
State kept by Activity belongs to the components inside it. If you need state to survive when the Activity itself is removed from the tree, Activity won't help. You would lift that state into a parent that stays mounted.
A common mistake: expecting no work at all
Hidden does not mean frozen. A hidden component keeps its state, and React can still re-render it when its props or context change. It does this at a lower priority than visible content. Because the DOM stays in the page, anything that makes noise or runs on its own, such as a <video> element, is still there. Later chapters cover both points. For now, remember that "preserved" describes data and DOM, not a guarantee that nothing happens.
Exercise
A Settings panel is wrapped in <Activity mode="hidden"> and holds a useState value with the user's half-typed name. The user switches to another tab, then comes back. Is the typed name still there? Would it still be there if the panel were rendered as {tab === 'settings' && <Settings />} instead? Explain both answers.
Show answer
With Activity, the name is still there. The component is not unmounted, so React keeps its state and its DOM nodes while it is hidden. With conditional rendering, the name is lost. A false condition unmounts Settings and discards its state and DOM. Switching back mounts a new instance with the initial state, an empty name.