Activity

React chapter 22 · React 19.2 · hidden UI · source: react.dev/reference/react/Activity

The problem

Unmount a tab and its typed text and counters are lost. Hide it with CSS and it stays alive, so its timers and network calls keep running in the background.

The pattern that fixes it

<Activity mode="hidden"> hides the tab but keeps its state, and pauses its effects until it is visible again. You get a remembered tab without the background work.

In plain words

When you switch tabs in a settings panel, something has to happen to the tab you left. You can throw it away (and lose what you typed) or just hide it (and let it keep working). Activity hides it and puts it to sleep.

Leaving a video game: unmounting is quitting without saving, CSS hiding is leaving the game running in the background draining the battery, and Activity is pausing it.

Terms

mounted
The component exists on the page and React holds its state.
effect
Code in useEffect that does work outside rendering, like a timer or a network subscription. It cleans up when it stops.
cleanup
The function an effect returns. React runs it when the effect stops, for example to clear a timer.
mode
Activity's prop: "visible" shows the children normally, "hidden" hides them and pauses their effects.

Going deeper

How does it work?

In "hidden" mode React keeps the children's state, hides their DOM, runs every effect cleanup (timers stop, subscriptions close), and defers their re-renders until nothing more urgent is waiting. Switching back to "visible" shows the DOM again and re-runs the effects, so the component picks up with its old state.

Pitfalls

Hidden children still hold their state in memory, so do not wrap hundreds of heavy tabs. Because effects are cleaned up and started again, an effect must be safe to stop and restart (clear the timer, close the socket). Things outside React, like a playing video element, may not pause by themselves: pause them in an effect cleanup. Hidden content is also removed from view and from assistive tech, which is what you want for a background tab.

When not to use it, and alternatives

If a tab should start fresh every time (a wizard step, a modal form), unmount it on purpose, because remembering state is a bug there. If you only need to remember a value, lift that state up or put it in the URL instead of keeping a whole subtree alive. Activity is not a performance tool by itself and not a replacement for the React Compiler: it keeps things alive on purpose, while the compiler skips repeated work.

Keep learning

Overview

effect started: timer on cleanup ran: timer off Your draft type something here Its state: the text above and the tick counter. Its effect: a timer that adds 1 every half second. Settings tab Editor tab Preview tab Preview tabYou are looking at Preview. What is the Editor doing right now? Timer ticks that fired while you were on the Preview tab ticks while hidden Effect timeline (real start and cleanup events from the Editor): waiting for the first effect… Real life: a tabbed settings panel with a draft editor. Type a draft, go to Preview, come back. How the hidden tab is handled Unmount it Hide with CSS gone when you leave: state is destroyed still mounted: effects keep running

Unmount: your draft and the counter are lost.

Every visit to Editor starts from zero, and the timeline shows the effect being cleaned up and started again from scratch.

CSS hide: the draft survives, but the timer never sleeps.

Stay on Preview a few seconds and watch "ticks while hidden" grow. A real tab might be polling a server or redrawing a chart for nobody. Type a draft, press Preview tab, wait 3 seconds, press Editor tab. Flip between Unmount it and Hide with CSS and compare the draft, the counter, and the bar. Same settings panel. Try all three ways to leave the Editor tab and compare. How the hidden tab is handled CSS hide Reset lab state kept, effects paused

Activity: draft and counter kept, timer asleep while hidden.

Leave the tab and "cleanup ran" appears in the timeline, with the bar staying at 0. Come back and the effect starts again, counting on from the old number.

CSS hide keeps state but never pauses work.

Leave the tab for a few seconds and watch the bar grow.

Unmount frees the work but forgets everything.

Come back and the draft and counter restart at zero. Compare the "ticks while hidden" bar across all three modes.

More courses

  • Jobs, linked to lessons — Browse jobs where every requirement links to the chapter that teaches it. See how ready you are, how long the gap takes to close, and which listings look stale.
  • Match a job post to courses — Paste a job post. Each skill is matched to a track here and put in learning order, foundations first.
  • Databases in depth — How a query becomes work. Indexes, isolation, locks, and the plans that explain why a page is slow — not an ORM tour.
  • System Design — Core concepts first (SQL vs NoSQL, CAP, indexing, replication, sharding), then designs grouped by what carries them: the database, Redis, Kafka, or many services.
  • NestJS: build a real API — One ticket-booking API, built in order: a plain Node server first, then NestJS modules, validation, a real database, and the features real clients lean on.
  • Microservices — Microservice patterns one diagram at a time: how to split a monolith, how services talk, and how data stays consistent without distributed transactions.
  • Kafka — A beginner-to-senior path through event logs, reliable delivery, production operations, and disaster recovery.
  • AI engineering for software engineers — Eight free reads, in order, for software engineers moving into AI: when to build an agent, how to build one, how to test it, and how the model works underneath.
  • Frontend system design — How a UI is sliced when it is a product, not a page: ownership of state, cache, routes, and what happens when the network lies.
  • TypeScript in depth — The type system as a design tool. Inference, narrowing, generics, and the errors that mean the model is wrong — not a catalog of utility types.
  • CSS in depth — See the box, then the page. Visual chapters from the box model through Flexbox surprises, Grid, selectors, and units, then backgrounds, shapes, and visual effects, each with a playground.
  • Accessibility — Foundations first (WCAG in plain words, semantic HTML, lists, alt text), then hands-on HTML labs, a full pass of WAI-ARIA 1.3, and a final fix-the-page challenge.
  • Docker — What an image is, why layers cache, and how containers find each other on a network.
  • Redis — Fifteen Redis patterns, one picture each: cache, session, lock, counter, rate limiter, queue, and when Redis is the wrong tool.
  • Kubernetes — Kubernetes from zero to operators: why it exists, Pods, Deployments and Services, then configuration, networking, storage, scheduling, scaling and security.
  • AWS — AWS networking in plain English. A VPC is a gated housing society: streets are subnets, the Internet Gateway is the main gate, and a route table is the signboard.
  • Networking: the NetRoute game — NetRoute is a packet logistics game. You wire up computers, switches, routers, firewalls and gateways, set each device, and watch glowing packets travel. If a packet gets dropped, the game shows you which header field was wrong.