Skip to content

Keeping Hidden UI Alive

The Course
•
15 min study

Tabs usually show one panel and remove the others. Removing a panel throws away its state and its DOM; showing it again builds both from scratch.

Tabs that forget React · 3 files
import { createRoot } from "react-dom/client"; import App from "./App.jsx"; import "./styles.css"; createRoot(document.getElementById("root")).render(<App />); import { useEffect, useState } from "react"; function Notes() { const [draft, setDraft] = useState(""); return ( <section className="panel"> <label> Draft note{" "} <textarea value={draft} onChange={(e) => setDraft(e.target.value)} rows={3} /> </label> </section> ); } function LiveTimer() { const [seconds, setSeconds] = useState(0); useEffect(() => { console.log("timer started"); const id = setInterval(() => setSeconds((s) => s + 1), 1000); return () => { console.log("timer stopped"); clearInterval(id); }; }, []); return <section className="panel"><p>Session: {seconds} s</p></section>; } export default function App() { const [tab, setTab] = useState("notes"); return ( <main> <h1>Study Hub</h1> <div role="tablist" aria-label="Sections"> <button role="tab" aria-selected={tab === "notes"} onClick={() => setTab("notes")}>Notes</button> <button role="tab" aria-selected={tab === "timer"} onClick={() => setTab("timer")}>Timer</button> </div> {tab === "notes" && <Notes />} {tab === "timer" && <LiveTimer />} </main> ); } body { font: 15px/1.5 system-ui, sans-serif; margin: 0; background: #f4f6f9; color: #1c2430; } main { max-width: 56rem; margin: 1rem auto; padding: 0 1rem; } h1 { margin: 0 0 0.5rem; font-size: 1.4rem; } input, select, button, textarea { font: inherit; } input, select, textarea { padding: 0.3rem 0.5rem; border: 1px solid #c4ccd6; border-radius: 6px; } button { padding: 0.25rem 0.7rem; border: 1px solid #c4ccd6; border-radius: 6px; background: white; cursor: pointer; } .toolbar { display: flex; flex-wrap: wrap; gap: 0.5rem; align-items: center; margin-bottom: 0.75rem; } .board { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 0.75rem; } .column { background: #e9edf2; border-radius: 10px; padding: 0.6rem; min-height: 6rem; } .column h2 { margin: 0 0 0.5rem; font-size: 0.95rem; } .count { color: #5c6878; font-weight: normal; } .cards { list-style: none; margin: 0; padding: 0; display: grid; gap: 0.4rem; } .card { background: white; border: 1px solid #d6dde6; border-radius: 8px; padding: 0.45rem 0.6rem; } .card button.title { border: 0; padding: 0; background: none; text-align: left; font-weight: 600; } .card.selected { outline: 2px solid #087ea4; } .muted { color: #5c6878; font-size: 0.85rem; } .panel { margin-top: 0.75rem; background: white; border: 1px solid #d6dde6; border-radius: 10px; padding: 0.75rem 1rem; } .panel h2 { margin: 0 0 0.25rem; font-size: 1.05rem; } .error { color: #b42318; } [role="tablist"] { display: flex; gap: 0.25rem; border-bottom: 1px solid #d6dde6; margin-bottom: 0.75rem; } [role="tab"] { border: 0; border-bottom: 2px solid transparent; border-radius: 0; background: none; padding: 0.4rem 0.8rem; } [role="tab"][aria-selected="true"] { border-bottom-color: #087ea4; color: #06617f; font-weight: 600; } .stars button { border: 0; background: none; padding: 0 0.1rem; font-size: 1.3rem; color: #b9c2cd; } .stars button[aria-checked="true"], .stars button.on { color: #d99a00; }
Runs with Vite on your computer: unzip, then npm install and npm run dev.

Type something in Draft note, switch to Timer for a few seconds, then back to Notes.

What you'll see

The draft is gone, and the console shows timer started and timer stopped. {tab === "notes" && <Notes />} unmounts Notes when you leave, so its state goes with it. The timer starts from 0 each visit, for the same reason.

Activity

React 19.2's <Activity> hides part of the tree without unmounting it:

JavaScript
import { Activity, useEffect, useState } from "react";
JSX
<Activity mode={tab === "notes" ? "visible" : "hidden"}>
  <Notes />
</Activity>
<Activity mode={tab === "timer" ? "visible" : "hidden"}>
  <LiveTimer />
</Activity>

Make the change and repeat the experiment.

What you'll see

The draft survives. The timer's count is kept too, but the console shows timer stopped when you leave the Timer tab, and timer started when you come back. While the count is hidden, it doesn't tick.

What hidden means

  • State is kept, and so is the DOM, hidden with display: none.
  • Effects are cleaned up while hidden, and set up again when visible. A hidden panel stops resources that its cleanup actually stops. Our timer cleanup clears the interval, so the timer pauses. An ignore flag drops a late response; it doesn't cancel the network request. Cancellation needs an API that supports it, such as fetch with an AbortController.
  • Updates continue at low priority. A hidden panel can still re-render, after everything visible, so it's up to date when shown.

That last point also allows pre-rendering: render a tab the user is likely to open next as hidden, so it appears instantly when they do.

When not to

Hidden panels still use memory. For dozens of tabs, or panels holding large data, unmount the ones that are rarely revisited. Activity is for a few panels whose state is worth keeping.

Exercise

A timer that keeps counting

Suppose the session timer should keep counting while hidden. Activity pauses its effect. Where would you keep the count instead?

Hint 1: the idea

A timer tied to a hidden component is cleaned up with that component. A timer that outlives the panel needs a different owner.

Hint 2: a first step

Something that doesn't belong to a component, and isn't paused when a component is hidden. Day 2 built one.

Show one solution

In an external store, ticked by an interval that lives outside React, like the Pomodoro checkpoint on Day 2. The component only reads the count with useSyncExternalStore. When it's hidden, it stops subscribing, but the store keeps counting, and showing it again reads the current value.

Was this article helpful?

Your response is saved on this device.