Skip to content

Topics

Jump to a topic.

Search questions

Find a question by title, tag, or summary.

How does <Activity> hide UI without losing its state?

Questions in this chapter (5)
  • Draft
  • medium
  • Askedsometimes
  • Verified October 2026

Answer

<Activity mode={isOpen ? 'visible' : 'hidden'}> is an alternative to {isOpen && <Sidebar />}. Stable since React 19.2, it has two modes:

  • hidden: React keeps the subtree's fibers, so state is preserved, and keeps the DOM nodes, so typed text, scroll position and a <video>'s timecode survive. It hides the top-level DOM nodes with display: none (React 19.3.1 sets it as !important inline) and cleans up the children's effects, layout effects included, as if they had unmounted. Hidden children still re-render when their props change, but at a lower priority, after the visible content has committed.
  • visible (the default): the nodes are shown again, with their previous state, and the effects are re-created.

Compare the alternatives:

  • Conditional rendering unmounts: state and DOM are gone and come back from scratch.
  • CSS-only hiding (hidden, display: none on a wrapper) keeps everything mounted, including effects, so subscriptions, timers and connections keep running in invisible UI, and every update renders it at full priority.

Activity sits between them: preserved like CSS hiding, quiet like unmounting.

Pre-rendering. A boundary that's hidden on its first render still renders its children, after the visible content and without running their effects. That lets a likely-next tab load its code and Suspense-based data in advance; data fetched in an effect isn't, since the effect doesn't run.

Because hidden DOM isn't destroyed, its side effects can continue. A hidden <video> keeps playing; pause it in an effect cleanup, preferably useLayoutEffect since the cleanup is tied to the UI being hidden. Effects with proper cleanup, the same ones that survive Strict Mode, already work under Activity.

Example

function Panel({ name }) {
  const [count, setCount] = useState(0);
  useEffect(() => {
    console.log(name, 'effect setup');
    return () => console.log(name, 'effect cleanup');
  }, [name]);
  return <button onClick={() => setCount(count + 1)}>{name}: {count}</button>;
}

function App() {
  const [show, setShow] = useState(true);
  return (
    <>
      <input type="checkbox" checked={show} onChange={() => setShow(!show)} />
      <Activity mode={show ? 'visible' : 'hidden'}>
        <Panel name="activity" />
      </Activity>
      {show && <Panel name="conditional" />}
    </>
  );
}
// Click both buttons twice, then untick:
// → conditional effect cleanup
// → activity effect cleanup
// Tick again:
// → activity effect setup
// → conditional effect setup
// Buttons now read "activity: 2" and "conditional: 0"

Chrome 156 and Firefox 159, development and production builds of React 19.3.1, no Strict Mode, trusted clicks. While hidden, the DOM still held <button style="display: none !important;">activity: 2</button>; the conditional button was gone.

Pitfalls

  • Expecting hidden effects to keep running. A chat connection or polling interval inside a hidden Activity is disconnected; keep work that must continue above the boundary.
  • Forgetting DOM side effects. Media keeps playing and iframes stay loaded while hidden; clean them up in an effect.
  • Rendering text alone in a hidden boundary. There's no element to put display: none on, so React renders nothing: <Activity mode="hidden"> around a component returning 'just text' left no text in the DOM.
  • Wrapping every hidden screen in Activity. Each keeps its whole DOM and state in memory and still re-renders on prop changes, just later; unmount what the user isn't likely to return to.

Follow-ups

Which effects does hiding clean up?
All of them. In the test, hiding ran the layout effect cleanup during the commit and the useEffect cleanup after it; showing re-ran both setups. State, refs and DOM nodes were kept.
What happens on the first render with mode="hidden"?
The children render after the visible part commits, their DOM is inserted already hidden, and no effects run. In the test, render hidden 0 came after the visible effects.
Why prefer it over lifting state up?
Lifting up keeps data but not DOM: scroll position, uncontrolled inputs, media playback and focus are lost on remount. Activity keeps both, at the cost of memory.

Exercises

In what order?

function Label({ name, count }) {
  console.log('render', name, count);
  return <p>{name}: {count}</p>;
}

function App() {
  const [count, setCount] = useState(0);
  useLayoutEffect(() => console.log('commit', count));
  return (
    <>
      <button onClick={() => setCount(count + 1)}>+1</button>
      <Label name="shown" count={count} />
      <Activity mode="hidden">
        <Label name="hidden" count={count} />
      </Activity>
    </>
  );
}
// Click +1.

Comments

Comments are GitHub Discussions: you sign in with GitHub to post or react.