Sub topic 11
Activity or something else? A decision guide
This chapter assumes you know useState, useEffect with cleanup, and the mode prop of Activity (React 19.2 or later). It helps you choose between four ways to hide UI: conditional rendering, Activity, CSS hiding, and lifting state up.
Start with what you need to keep
Hiding UI can preserve three different things. The right tool depends on which of them you need.
- Component state: values held in
useStateoruseReducer, such as text typed into a field. - DOM: the rendered nodes, including scroll position and uncontrolled input values.
- Running work: timers, subscriptions, and network connections set up in Effects.
Each option treats these differently.
| Approach | State kept | DOM kept | Effects while hidden | Render cost while hidden |
|---|---|---|---|---|
| Conditional rendering (`{show && <X />}`) | No | No | Unmounted | None |
| `Activity` with `mode="hidden"` | Yes | Yes (not displayed) | Cleaned up | Low-priority re-renders |
| CSS (`display: none`) | Yes | Yes | Still running | Full, at normal priority |
| Lifting state up | Only the lifted state | No | Unmounted | None |
The options in practice
Conditional rendering
This is the default and usually the right choice. If a user closing a dialog should get a fresh dialog next time, unmounting is the behavior you want. It also costs nothing while the content is gone.
Use it when state should reset, when the content is rarely revisited, or when the tree is large and expensive to keep.
Activity
Choose Activity when users move back and forth between sections and expect to find them as they left them. Typical cases are tabs, multi-step forms, sidebars, and a page the user is likely to open next.
import { Activity, useState } from 'react';
function Filters() {
const [query, setQuery] = useState('');
return <input value={query} onChange={(e) => setQuery(e.target.value)} placeholder="Search" />;
}
export default function Panel() {
const [open, setOpen] = useState(true);
return (
<>
<button onClick={() => setOpen(!open)}>Toggle filters</button>
<Activity mode={open ? 'visible' : 'hidden'}>
<Filters />
</Activity>
</>
);
}Because Effects are cleaned up while hidden, timers and subscriptions stop. This is the main advantage over CSS hiding. The cost is that your Effect cleanup must be correct, and media or other DOM side effects need explicit handling (see the earlier chapters on useLayoutEffect and video.pause()).
CSS hiding
display: none leaves the component fully mounted. Its Effects keep running, and it re-renders at normal priority whenever its props or state change. Choose it only when you want that: a live chart that must keep receiving data, or a trivial element where Activity adds nothing. It is also the only option that needs no React-version support.
Lifting state up
If only one or two values need to survive, move them into a parent that stays mounted and pass them down. The child can still unmount, so there is no hidden tree to pay for.
import { useState } from 'react';
function Draft({ text, onChange }) {
return <textarea value={text} onChange={(e) => onChange(e.target.value)} />;
}
export default function App() {
const [show, setShow] = useState(true);
const [text, setText] = useState('');
return (
<>
<button onClick={() => setShow(!show)}>Toggle draft</button>
{show && <Draft text={text} onChange={setText} />}
</>
);
}This keeps the text but not the DOM, so scroll position and focus are lost. It also makes the parent own state it would otherwise not care about. It works well when the preserved data is small and the parent legitimately needs it, for example to save it or to validate it.
A decision flow
Common mistakes
- Wrapping everything in
Activity. Every hidden tree stays in memory and re-renders at low priority. Apply it to the few sections users revisit. - Expecting a reset. Reopening an
Activityshows the old state. If the user should start fresh, use conditional rendering or give the component a changingkey. - Ignoring cleanup. An Effect with no cleanup keeps its work running after the boundary is hidden. Write cleanup as if the component might unmount, because React treats hiding the same way for Effects.
- Forgetting DOM side effects. A hidden video or audio element stays in the page, so pause it in a layout Effect cleanup.
Exercise
A dashboard has a chart that must keep receiving live data while the user is on another tab, and a settings form whose typed values must survive tab switches. Which approach fits each, and why?
Show answer
Keep the chart mounted and hide it with CSS, because Activity cleans up its Effects and would stop the data subscription while hidden. Wrap the settings form in Activity, because it needs its state and DOM kept and has no background work to continue. If only one or two form values mattered, lifting them into the parent would also work.