Keep React Drafts When a Panel is Hidden

Close a review panel halfway through a note. Open it again. If the note has gone, the interface has interpreted "hide this" rather more aggressively than the person using it.
Keeping the panel mounted avoids losing its local state, but a hidden panel can still have subscriptions running. React 19.2's new Activity component gives us another choice: preserve the state whilst cleaning up the hidden subtree's Effects.
I've used a small review editor to compare those choices. It has a draft textarea, a notice subscription, and two fictional records. The interesting checks are what survives hiding, what stops running, and whether the right draft appears when the selected record changes.
Start with the Behaviour You Already Have
My conditional version renders the editor only when the panel is visible. Hiding removes the component. Its subscription cleanup runs, and reopening creates a fresh editor with an empty draft.
The second version leaves it mounted inside a wrapper with the HTML hidden attribute. The draft survives, but the subscription remains connected. Publishing a notice whilst it is hidden still delivers that notice to the component.
Both results can be reasonable. A temporary panel might deliberately reset on close. A hidden view might need to keep collecting updates. The mistake is choosing either implementation without deciding what "hide" means for that particular screen.
For this editor, I want to retain the unfinished text and stop its subscription whilst it is out of use. That gives the comparison an observable target.
Preserve the Editor with Activity
React 19.2 introduces Activity with visible and hidden modes:
<Activity mode={visible ? 'visible' : 'hidden'}>
<Editor key={entity} entity={entity} paused={false} close={close} />
</Activity>When hidden, the editor's DOM remains but is not displayed, and its Effects are cleaned up. Showing it restores the previous state and runs those Effects again. In my example, the textarea retains its draft and the subscription count falls from one to zero whilst hidden.
That does not mean the hidden subtree has ceased to exist or can never render. Hidden work can still respond to changed props at a lower priority. I would not use an Effect running as a prerequisite for producing the correct initial render of a hidden component.
My comparison uses the same editor in each mode, including the same subscription code. Changing the wrapper should not quietly change the work being measured.
Reconnect to the Current Notice
Stopping the subscription leaves a small question: what happens to notices published during the gap?
My fictional source retains the current notice for each record. On connection, the editor reads that value and subscribes for subsequent changes:
useLayoutEffect(() => {
if (paused) return;
setNotice(source.read(entity));
return source.subscribe(entity, setNotice);
}, [entity, paused]);The layout effect lets this small display catch up before paint when it becomes visible. The source is an in‑memory synchronous fixture; this is not a general replacement for a concurrent external‑store subscription API or a network reconnect protocol.
The test publishes a new notice whilst the Activity is hidden. It records no delivery to the disconnected editor. Reopening reads the retained value and displays it. A second test starts with the panel hidden, changes the source before its first appearance, and checks the same outcome.
This distinction matters for real sources. Some retain a current value, some replay events, and some lose messages once nobody is listening. Activity does not choose that policy. The source and its consumer still need to agree on how to catch up.
Keep the Existing CSS Repair in the Comparison
I also have a fourth mode: the CSS‑hidden editor receives an explicit paused prop. That prop triggers the subscription cleanup whilst preserving the component's local state. For the draft and notice behaviour, it passes the same checks as Activity.
That is useful evidence when considering a migration. If an existing component already pauses the right work, there may be no visible bug to fix. Activity can express the lifecycle boundary without passing a visibility prop through every interested child, but replacing working code still needs a reason.
I covered the importance of returning the actual cleanup in my React memory‑leak guide. A wrapper cannot remove a listener whose owner never supplied the removal step.
Repeated opening and closing is part of my test too. Each reopened editor has one subscription. A panel which appears to work once can still leave several listeners behind after a few visits.
Preserve the Right Record's State
Preserving state becomes a bug if the identity is wrong. I type a note for record A, hide the editor, switch to record B, and reopen it. B must start with its own empty draft and current notice.
The key={entity} above makes that change of identity explicit. My deliberately broken control uses the same key for both records. It successfully preserves A's draft and then displays it under B's heading. An impressive demonstration of the wrong requirement.
My article about React keys covers this more generally. The important point here is that visibility and identity are separate decisions. Keeping the same record hidden should preserve its editor; selecting a different record should not borrow the previous record's local draft.
The example does not keep a draft cache for every record. Switching away resets that component's state. If drafts must survive revisiting multiple records, their storage needs to live somewhere with that longer lifetime.
Handle Focus and Media Explicitly
Hiding a focused textarea needs a sensible destination for focus. In my panel, Escape moves focus to the Show panel button before hiding. Opening moves it back into the visible editor. The browser checks the active element after each action; preserving a DOM node does not itself establish a useful focus policy.
Media needs attention as well. My fixture includes a muted, silent audio loop. Without owned cleanup, it remains unpaused and its playback position advances after the Activity becomes hidden. The DOM is still there.
For the corrected variant, a layout effect captures the audio element and pauses it during cleanup. The test checks the actual media state after hiding. This follows the distinction in React's Activity guidance: hiding retained DOM can leave its own behaviour running, even after ordinary subscriptions have been cleaned up.
A Preserved Draft Has a Limited Lifetime
My Activity comparison includes the visibility modes, the constant‑key failure, the media control, and the browser checks. It also reloads the page after typing a draft. The draft disappears, as it should in an example with no persistent storage.
Activity is useful for temporarily leaving a view and returning to the same in‑memory state. If "keep my draft" also means surviving a refresh, navigation away, or a closed browser, there is another piece of work to do. Keeping a component hidden cannot provide that persistence on its own.
Postscript
November 2026: My download now checks React 19.3.1 alongside the original 19.2.0 release. The draft, subscription, focus, and record‑identity comparisons still agree in those builds. The package uses a current Node 22 toolchain and records its actual browser checks; it is not a recovered October 2025 dependency tree. The media control remains useful too: retaining a hidden element still makes its cleanup the component's responsibility.