← Back to React, page 5

react-0096 · Hard senior

What does the Activity component (React 19.2) do, and how does it differ from conditional rendering or hiding with CSS?

Activity lets you hide part of the UI without losing its state. With mode="hidden", React keeps the component state and DOM, hides the content, unmounts its effects (cleanups run) and renders it at low priority. With mode="visible" it shows again and re-creates the effects. Conditional rendering destroys state, and plain CSS hiding keeps effects running and updates at normal priority.

Full answer

Tabs, wizards and sidebars often need the inactive view to keep its state: a half-filled form, a scroll position, a loaded list. Before React 19.2 there were two options. Conditional rendering ({tab === 'notes' && <Notes />}) unmounts the component and loses state. Keeping it mounted but hidden with CSS preserves state, but its effects keep running (subscriptions, timers, fetches) and it re-renders at normal priority although nobody sees it.

<Activity mode="visible" | "hidden"> was added in React 19.2 and sits between them.

  • In hidden mode React keeps the fiber's state and DOM, but sets the content hidden (with display: none), and unmounts its effects, so subscriptions are cleaned up. Updates for hidden content are rendered at a low priority when React is idle.
  • When mode returns to visible, React shows the content and runs the effects again. State is as you left it.

Use cases: tabs and navigation where users go back and forth, preserving form input and scroll position, pre-rendering a screen the user is likely to open next (hidden Activity can render data in the background, so the switch is quick), and keeping expensive subtrees alive without paying for their effects.

Important details. Effects clean up when hidden, so code that must keep running in the background (a socket for notifications) should live outside the hidden subtree. Layout effects clean up too. DOM state that React does not control (video playback, an uncontrolled input's value in some cases) needs thought: the elements stay in the DOM but are hidden. Hidden content still occupies memory, so do not keep dozens of heavy screens alive. Server rendering renders hidden children at lower priority and without blocking visible content.

Compare with key change (reset on purpose) and with state lifted to a parent (persist data, but not DOM or scroll). Choose Activity when the whole subtree's state should survive.

The example has two tabs and logs effect setup and cleanup.

jsx
import { Activity, useEffect, useState } from 'react';

function Notes() {
  const [text, setText] = useState('');

  useEffect(() => {
    console.log('Notes effect started');
    return () => console.log('Notes effect cleaned up');
  }, []);

  return (
    <section data-tab="notes">
      <input aria-label="Notes" value={text} onChange={(e) => setText(e.target.value)} />
    </section>
  );
}

function Settings() {
  return <section data-tab="settings">Settings content</section>;
}

export default function Tabs() {
  const [tab, setTab] = useState('notes');

  return (
    <div>
      <button onClick={() => setTab('notes')}>Notes</button>
      <button onClick={() => setTab('settings')}>Settings</button>
      <Activity mode={tab === 'notes' ? 'visible' : 'hidden'}>
        <Notes />
      </Activity>
      <Activity mode={tab === 'settings' ? 'visible' : 'hidden'}>
        <Settings />
      </Activity>
    </div>
  );
}

The check typed in the Notes input, switched to Settings, and came back. The typed text is still there, and the console lines show the effect cleanup and restart. Output of the run: Notes effect started typed in the Notes tab | input value: "draft text" Notes effect cleaned up Settings tab selected (Notes hidden) | input value: "draft text" Notes effect started Notes tab selected again | input value: "draft text" hidden attribute states: notes=, settings=none Notes effect cleaned up

Key points

  • Activity hidden keeps state and DOM but cleans up effects and renders updates at low priority.
  • Conditional rendering loses state. CSS hiding keeps effects running.
  • Visible mode shows the content again and re-runs effects with the preserved state.
  • Good for tabs, wizards and pre-rendering likely next screens.
  • Do not put needed background work (sockets) inside hidden content, and do not keep too many heavy screens alive.

Common mistakes

  • Putting a socket or subscription that must stay alive inside a hidden Activity.
  • Using conditional rendering and then wondering why form input is lost.
  • Keeping many heavy screens alive and using too much memory.
  • Assuming CSS-hidden content stops running effects.
  • Using Activity where a key reset is the intended behaviour.

Follow-ups

  • How would you pre-render the next tab with Activity?
  • How does Activity relate to Suspense and transitions?
  • What happens to layout effects in a hidden Activity?

Tips

  • Compare the three options (unmount, CSS hide, Activity). The comparison is the answer.
  • Mention that effects are cleaned up while hidden.
  • Say it needs React 19.2 or later.