Sub topic 4
What stays: state and DOM while hidden
This chapter assumes you know useState, props, and how React mounts and unmounts components. It also assumes you can use React 19.2 or later, where Activity is available. The previous chapter showed how to switch an Activity boundary between visible and hidden. This chapter covers what React keeps while a boundary is hidden. Effects are covered in the next chapter, so they appear here only where they affect the picture.
The short version
When an Activity boundary is hidden, React keeps three things:
- The state of every component inside it, including
useStateanduseReducervalues. - The DOM nodes those components rendered, which React hides with CSS instead of removing.
- The component tree itself, so each component keeps its identity in the fiber tree.
Compare this with conditional rendering. When {show && <Draft />} becomes false, React unmounts Draft. It discards the state and removes the DOM nodes. When show becomes true again, React mounts a fresh Draft with initial state.
State survives because the component never unmounts
React ties state to a component's position in the tree. A hidden Activity keeps its children at that position, so their state stays attached.
Here is a complete example with two tabs. Each tab holds its own state.
import { Activity, useState } from 'react';
function Counter() {
const [count, setCount] = useState(0);
return (
<button onClick={() => setCount(count + 1)}>
Clicked {count} times
</button>
);
}
function Notes() {
const [text, setText] = useState('');
return (
<textarea
value={text}
onChange={(e) => setText(e.target.value)}
placeholder="Notes"
/>
);
}
export default function App() {
const [tab, setTab] = useState('counter');
return (
<>
<button onClick={() => setTab('counter')}>Counter</button>
<button onClick={() => setTab('notes')}>Notes</button>
<Activity mode={tab === 'counter' ? 'visible' : 'hidden'}>
<Counter />
</Activity>
<Activity mode={tab === 'notes' ? 'visible' : 'hidden'}>
<Notes />
</Activity>
</>
);
}Click the counter a few times, switch to Notes, type something, and switch back. The counter still shows your count, and the textarea still holds your text. If you replace both Activity wrappers with {tab === 'counter' && <Counter />} and {tab === 'notes' && <Notes />}, both values reset on every switch.
Both versions use the same useState calls. Only the boundary differs. Unmounting is what resets state, and Activity prevents the unmount.
The DOM survives too
State is not the only thing a user builds up in a screen. The browser keeps its own state on DOM nodes:
- The value of an uncontrolled input, one with no
valueprop. - The scroll position of a list or a
<textarea>. - Whether a
<details>element is open. - Text selection and the checked state of uncontrolled checkboxes.
- The playback position of a
<video>or<audio>element.
Conditional rendering destroys all of this, because the nodes are removed from the page. Activity keeps it, because the nodes stay in the document. React sets display: none on the top-level DOM nodes inside a hidden boundary. The browser stops painting them but does not discard them.
This example uses only DOM state, with no useState at all.
import { Activity, useState } from 'react';
function Faq() {
return (
<div style={{ height: 80, overflow: 'auto', border: '1px solid gray' }}>
<details>
<summary>What is Activity?</summary>
<p>A component that can hide its children without unmounting them.</p>
</details>
<input defaultValue="" placeholder="Uncontrolled search" />
<p>Line 1</p>
<p>Line 2</p>
<p>Line 3</p>
<p>Line 4</p>
<p>Line 5</p>
<p>Line 6</p>
</div>
);
}
export default function App() {
const [showFaq, setShowFaq] = useState(true);
return (
<>
<button onClick={() => setShowFaq(!showFaq)}>Toggle FAQ</button>
<Activity mode={showFaq ? 'visible' : 'hidden'}>
<Faq />
</Activity>
</>
);
}Open the <details> element, type in the input, and scroll the box. Hide and show the FAQ. The element is still open, the input still has your text, and the scroll position is the same. React never wrote those values, so it could not restore them if the nodes were recreated. They persist only because the nodes persist.
Preserved state is still live state
A hidden boundary is not frozen. Components inside it remain part of the React tree, so a few things still reach them:
- Props from the parent. If the parent re-renders and passes new props, the hidden component re-renders with them. React schedules that work at a lower priority than visible updates, so the visible screen stays responsive. The last chapter in this tutorial's priority section covers the scheduling in detail.
- Context. A hidden component that reads a context value re-renders when that value changes.
- Its own state updates. If something calls a
setfunction from inside a hidden tree, React processes the update. You normally cannot click a hidden element, but a timer or a callback you set up earlier can still trigger it.
The consequence is that hidden state can change while the user is away. When they return, they see the latest data computed from current props, together with the state they left behind.
import { Activity, useState } from 'react';
function Preview({ name }) {
const [expanded, setExpanded] = useState(false);
return (
<section>
<h2>Hello, {name}</h2>
<button onClick={() => setExpanded(!expanded)}>
{expanded ? 'Collapse' : 'Expand'}
</button>
{expanded && <p>Extra details for {name}.</p>}
</section>
);
}
export default function App() {
const [name, setName] = useState('Ada');
const [visible, setVisible] = useState(true);
return (
<>
<button onClick={() => setVisible(!visible)}>Toggle preview</button>
<button onClick={() => setName(name === 'Ada' ? 'Grace' : 'Ada')}>
Change name
</button>
<Activity mode={visible ? 'visible' : 'hidden'}>
<Preview name={name} />
</Activity>
</>
);
}Expand the preview, hide it, click "Change name", then show it again. The preview is still expanded (preserved state) and it shows the new name (updated props).
Identity rules still apply
Activity keeps state because the component stays at the same place with the same type and key. Anything that changes that identity still resets state, even inside a boundary.
- Changing the
keyon a component insideActivitymounts a new instance. Akeyon theActivityelement itself does the same for everything inside it. - Rendering
Activityconditionally, as in{ready && <Activity>...</Activity>}, unmounts the boundary and everything in it whenreadybecomesfalse. - Moving the same component to a different position in the tree creates a new instance.
You can use this on purpose to reset a screen. Here a key gives a form a fresh start while Activity still preserves it across hides.
import { Activity, useState } from 'react';
function Form() {
const [value, setValue] = useState('');
return <input value={value} onChange={(e) => setValue(e.target.value)} />;
}
export default function App() {
const [visible, setVisible] = useState(true);
const [formId, setFormId] = useState(0);
return (
<>
<button onClick={() => setVisible(!visible)}>Hide or show</button>
<button onClick={() => setFormId(formId + 1)}>Reset form</button>
<Activity mode={visible ? 'visible' : 'hidden'}>
<Form key={formId} />
</Activity>
</>
);
}Hiding and showing keeps the text. "Reset form" changes the key, so React mounts a new Form with an empty input.
What the two modes do to each layer
The table summarizes what happens to each layer of a screen when you compare a visible boundary, a hidden one, and an unmounted component.
| Layer | `visible` | `hidden` | Unmounted (conditional rendering) |
|---|---|---|---|
| Component state | Live | Kept | Lost |
| DOM nodes | Painted | Kept, `display: none` | Removed |
| DOM-only state (scroll, uncontrolled input) | Live | Kept | Lost |
| Re-render on prop change | Normal priority | Lower priority | Not applicable |
| Effects | Running | Cleaned up | Cleaned up |
The last row of the table is the subject of the next chapter. For now, treat it as a warning. Preserved state does not mean preserved side effects.
Common mistakes
Expecting a hidden component to be idle. It keeps its state and can still re-render when its props or context change. If the component does expensive work during render, hiding it does not remove that cost. Hidden renders are only scheduled at lower priority.
Expecting a reset on show. Some developers assume that a screen opens fresh each time it becomes visible. With Activity it does not. If a form should clear after submission, clear its state explicitly or change its key.
Reading stale state from derived values. State is preserved, but data you derived from the outside world may be out of date when the user returns. For example, a "last updated 3 minutes ago" label computed once at render time stays frozen while hidden. Recompute such values from current inputs, or refresh them in an Effect that runs when the boundary becomes visible again.
Assuming hiding frees memory. The state, the fiber tree, and the DOM nodes all remain allocated. A long list of tabs that each hold large data stays in memory until the boundary is unmounted. The cost chapter covers when this matters.
Relying on state that should not survive. Sensitive values such as a password field or a one-time code also stay in memory and in the hidden DOM. If a screen should not keep them after the user leaves, clear them explicitly or unmount that part with conditional rendering.
Checking it yourself
You can see the difference between hidden and unmounted in the browser's developer tools. Open the Elements panel and toggle a boundary. For a hidden Activity, the nodes remain in the document with display: none in their style. For a conditionally rendered component, the nodes disappear. The React DevTools Components panel also continues to list a hidden component, which confirms it is still mounted in the tree.
A quick log in a component body helps too. Put console.log('render', name) in Preview from the earlier example and change the name while the boundary is hidden. You will see the hidden component render with the new prop, even though nothing on screen changes.
Summary
Activity in hidden mode keeps a subtree's component state and its DOM nodes. React hides the nodes with display: none instead of removing them, so DOM-only state such as uncontrolled input text, scroll positions, and open <details> elements also survives. The hidden tree still receives prop and context updates, at lower priority. State resets only when the component's identity changes, through a different key, position, or type, or when the boundary itself is removed.
Exercise
A Settings panel is wrapped in <Activity mode={tab === 'settings' ? 'visible' : 'hidden'}>. It contains a controlled <input> backed by useState and an uncontrolled <input defaultValue="">. The user types into both, switches to another tab, and comes back. What does each input show, and what would each show if the panel used {tab === 'settings' && <Settings />} instead?
Show answer
With Activity, both inputs still show the typed text. The controlled input keeps it in preserved component state, and the uncontrolled input keeps it because its DOM node stays in the page with display: none. With conditional rendering, Settings unmounts, so the state is lost and the DOM node is removed. After returning, the controlled input is empty and the uncontrolled input shows its defaultValue of an empty string.
Exercise
A Form inside a hidden Activity needs to be emptied after the user submits it successfully, even though the boundary stays mounted. Name two ways to do this.
Show answer
Reset the form's state explicitly, for example by calling setValue('') after the submit succeeds. Or give the form a key held in parent state and change it, for example <Form key={formId} /> with setFormId(formId + 1). The new key makes React mount a fresh Form with initial state inside the same Activity boundary.
Exercise
A parent passes a name prop to a component inside a hidden Activity, then changes name while the boundary is hidden. When the user shows the boundary again, which value does the component display, and does it keep its earlier state?
Show answer
It displays the new name, because hidden components still re-render when props change, at lower priority. It also keeps its earlier state, because the component never unmounted and its identity did not change.