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.
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:
import { Activity, useEffect, useState } from "react";<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
ignoreflag drops a late response; it doesn't cancel the network request. Cancellation needs an API that supports it, such asfetchwith anAbortController. - 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.
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.
Completion is saved in this browser only. It does not measure mastery.