Preserve hidden UI state with Activity
Preserve local state when hiding an interface
Activity is a stable React 19.2+ boundary that can hide a part of an interface while retaining its state. Its hidden children are visually suppressed and their Effects are cleaned up. Revealing them restores the preserved state and establishes Effects again. Hidden rendering may continue at lower priority. This differs both from unmounting a subtree and from simply applying CSS display:none. Use matched patched React/React DOM versions with Activity available; this is not an API for React 18 or React 19.0/19.1.
import React,{Activity,useState,useEffect} from 'react';function Notes(){const [note,setNote]=useState('');return <section><label>Exhibit note <textarea value={note} onChange={e=>setNote(e.target.value)}/></label></section>;}export default function App(){const [open,setOpen]=useState(true);return <section><button aria-expanded={open} aria-controls="notes" onClick={()=>setOpen(v=>!v)}>{open?'Hide':'Show'} notes</button><div id="notes"><Activity mode={open?'visible':'hidden'}><Notes/></Activity></div></section>;}Enter a synthetic note, hide the editor and show it again. The note should remain intact. The surrounding button stays accessible. If Notes were removed with open && <Notes/>, its local state would be lost. Activity does not save to disk or protect against a page reload, and changing the key can reset state. Hidden state is not a privacy boundary; avoid retaining sensitive information unnecessarily.
Clean up subscriptions when hidden
import React,{Activity,useState,useEffect} from 'react';function createFeed(){ const listeners=new Set(); return {subscribe(fn){listeners.add(fn);return ()=>listeners.delete(fn);},publish(value){for(const fn of [...listeners])fn(value);},count(){return listeners.size;}};}const feed=createFeed();function Listener(){const [message,setMessage]=useState('No messages');useEffect(()=>feed.subscribe(setMessage),[]);return <p role="status">{message}</p>;}export default function App(){const [open,setOpen]=useState(true);return <section><button onClick={()=>setOpen(v=>!v)} aria-expanded={open} aria-controls="feed">Toggle feed</button><button onClick={()=>feed.publish('Cedar display updated')}>Publish update</button><button onClick={()=>console.log('Active listeners:',feed.count())}>Inspect listeners</button><div id="feed"><Activity mode={open?'visible':'hidden'}><Listener/></Activity></div></section>;}After Effects settle, the visible listener should be subscribed once. Hiding should remove its subscription; showing should recreate it. Messages published while hidden are intentionally lost in this event-only fixture. Its last displayed value remains in state. A real external store needs a current snapshot when subscribing again, for example useSyncExternalStore. Development StrictMode can run additional setup and cleanup cycles, so assert the final active count rather than exact lifecycle logs. The feed helper is tested locally; Activity integration is not.
Offer a compatibility alternative
import React,{useState} from 'react';function Notes({note,onChange}){return <label>Exhibit note <textarea value={note} onChange={e=>onChange(e.target.value)}/></label>;}export default function App(){const [open,setOpen]=useState(true);const [note,setNote]=useState('');return <section><button aria-expanded={open} aria-controls="notes" onClick={()=>setOpen(v=>!v)}>Toggle notes</button><div id="notes">{open&&<Notes note={note} onChange={setNote}/>}</div></section>;}This React 18+ alternative keeps only the note in its parent, so the editor can be unmounted without losing that text. It does not preserve all descendant state or prerender hidden content. A hidden Activity can prepare lazy or Suspense-compatible resources, but does not automatically fetch data from Effects. Decide based on memory, state ownership, effect lifetime and compatibility rather than wrapping every panel. Practice: compare both note implementations, check keyboard access after hide/show, and verify that the hidden feed releases its subscription.