React / 93
Portals and Activity
createPortal renders a modal somewhere else in the DOM while it stays in your React tree. Activity hides UI without unmounting it, so its state survives the switch.
createPortal: render somewhere else
createPortal(children, domNode) from react-dom renders children into another DOM node, usually document.body, that must already exist.
import { useState } from "react";
import { createPortal } from "react-dom";
export default function ConfirmButton() {
const [open, setOpen] = useState(false);
return (
<div className="card">
<button onClick={() => setOpen(true)}>Delete order</button>
{open && createPortal(
<div className="modal" role="dialog" aria-modal="true">
<p>Delete this order?</p>
<button onClick={() => setOpen(false)}>Cancel</button>
</div>,
document.body
)}
</div>
);
}React tree
decides props, context and event bubbling
DOM tree
decides layout, clipping and stacking
That's the key idea: only the DOM position changes. Context from above still reaches the modal, and a click inside it bubbles to the card's onClick, even though the DOM nodes aren't nested.
Escape the overflow-hidden box
Try itclicks the card's onClick heard
card · overflow: hidden · height 160px
Portals are also the usual answer for tooltips, toasts and dropdowns that must float above everything. When you build a modal, handle accessibility too: role="dialog", move focus into it, close on Escape and return focus when it closes.
Activity: hide without unmounting
<Activity> (stable since React 19.2) takes a mode of "visible" or "hidden":
import { Activity, useState } from "react";
export default function Tabs() {
const [tab, setTab] = useState("menu");
return (
<>
<TabButtons tab={tab} onChange={setTab} />
<Activity mode={tab === "menu" ? "visible" : "hidden"}>
<Menu />
</Activity>
<Activity mode={tab === "cart" ? "visible" : "hidden"}>
<CartNote />
</Activity>
</>
);
}When mode="hidden", React:
Hides the DOM
It setsdisplay: noneon the children. The DOM nodes stay in the page.Keeps the state
Typed text, picked filters, open accordions: all there when you come back.Cleans up the effects
Subscriptions and timers stop, exactly like an unmount. They set up again when the content becomes visible.Updates it at low priority
Hidden children still re-render on new props, but only after the visible UI is done.
Conditional render
{show && <Cart />}- state
- lost
- effects
- cleaned up
- DOM
- removed
CSS only
<Cart className="hidden" />- state
- kept
- effects
- keep running
- DOM
- kept, hidden
Activity
<Activity mode="hidden">- state
- kept
- effects
- cleaned up, re-created on show
- DOM
- kept, display: none
Hide a tab without losing it
Try itNoteTab's effect
Because hidden content still renders in the background, you can also use mode="hidden" to pre-render a tab the user is likely to open next. Data read with use(promise) loads during that pre-render. Data fetched in an effect doesn't, because effects don't run while hidden.
Gotchas
Recall
Can you answer these?
0/4
Answer out loud (or in your head) before you tap. That's what makes it stick.
Saved on this device only. No login.
